AVPlayerViewController memory leaks

Hi guys,

I am having memory leaks using AVPlayerViewController with iOS 12.4 and XCode 10.3.
I built a test to reproduce it.

Just create a new project using Single View App template, then use this code in the ViewController.swift

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        DispatchQueue.main.asyncAfter(deadline: .now() + 7.0) {
            self.playerViewController = AVPlayerViewController()
            let player = AVPlayer(url: URL(string: "https://wolverine.raywenderlich.com/content/ios/tutorials/video_streaming/foxVillage.m3u8")!)
            self.playerViewController.player = player
            NotificationCenter.default.addObserver(self, selector: #selector(self.playerDidFinishPlaying(note:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.playerViewController.player?.currentItem)
            self.present(self.playerViewController, animated: true) {
                player.play()
            }
        }
    }
    
    @objc func playerDidFinishPlaying(note: NSNotification) {
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.playerViewController.player?.currentItem)
        self.playerViewController.dismiss(animated: true)
    }


The code presents (with a 7 seconds delay) an AVPlayerViewController with a video url (you can use another one if you prefer) and close it automatically when the video ends. Since the code is in viewDidAppear the process is repeated in loop.
The memory leaks appear everytime the AVPlayerViewController is dismissed and memory keeps growing and is never released.


The leaks are present even if you don't do the automatic dismiss using the AVPlayerItemDidPlayToEndTime notification.
Just add this code to your viewcontroller and dismiss the AVPlayerViewController using the close button.
The problem of memory leaks persists.

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
            self.playerViewController = AVPlayerViewController()
            let player = AVPlayer(url: URL(string: "https://wolverine.raywenderlich.com/content/ios/tutorials/video_streaming/foxVillage.m3u8")!)
            self.playerViewController.player = player
            self.present(self.playerViewController, animated: true) {
                player.play()
            }
        }
}


If I am doing something wrong please help me understand what is it.


Thank you for your time.
Gio

Replies

I am also seeing this. Did you ever resolve this issue Gio ?