简体   繁体   中英

How to stop AVQueuePlayer memory leak?

I'm using Xcode 7, Swift and iOS 9.0.

If I...

  • Create AVQueuePlayer with some items
  • Start to play it
  • Then removeAllItems() ... the memory doesn't get released.

If call this function, it starts to hog up memory:

var queuePlayer: AVQueuePlayer!

func startAgain(){

    if queuePlayer != nil{
        queuePlayer.pause()
        queuePlayer.removeAllItems()
        queuePlayer = nil
    }

    var items: [AVPlayerItem] = []

    for _ in 1 ... 10 {
        items.append(AVPlayerItem(URL: NSBundle.mainBundle().URLForResource("Music", withExtension: "mp3")!))
    }

    queuePlayer = AVQueuePlayer(items: items)
    queuePlayer.play()

    NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "startAgain", userInfo: nil, repeats: false)
}

I even subclassed AVPlayerItem and AVURLAsset to see if they get deinitialized... And they do! So I have no idea why this is happening.

Using Instruments I can see that VM: Performance tool data is what is using the memory and it never gets released.

Do you have any ideas how to free up the memory?

What should I do with AVQueuePlayer in order it to release the memory?

You need to take care of releasing the current array var items: [AVPlayerItem]. It would be better to create it as a class property. From your code, the timer is not invalidated and therefore it keeps the old array. The best practice is to release the old timer before creating a new timer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM