Memory leak in iOS, AVPlayer is never deallocated

僤鯓⒐⒋嵵緔 提交于 2019-12-06 20:49:16

问题


I used the AVPlayerDemo sample from the Apple docs and wrote my own UI on top of it to play videos selected from a UITableViewController. Now, the problem is that there's a memory leak here somewhere which I can't find out. The problem is that the AVPlayer object is not being dealloced, I guessed this because every time a press back button and select a new video to play, there is a huge jump in the total memory consumed by the app which is show here:

The first time the video is player, the memory usage is 36.6MB, now for the second time:

Here it has jumped to 58.2MB, and keeps on increasing every time i go back and play the video again or a different video.

I have tried using Instruments with Leaks but haven't yet been able to figure out whats wrong with it.

Heres the whole Controller file code.

//EDIT

-(void) viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

    if(_player.rate == 1.0){
        [_player pause];
    }

    [idleTimer invalidate];

    if(mTimeObserver){
        [_player removeTimeObserver:mTimeObserver];
        mTimeObserver = nil;
    }
    [_playerItem removeObserver:self forKeyPath:kStatusKeyT];
    [[NSNotificationCenter defaultCenter] removeObserver:self                                                 name:AVPlayerItemDidPlayToEndTimeNotification object:_playerItem];


    _player = nil;
    _playerItem = nil;
    idleTimer = nil;
    _tapGestureRecognizer = nil;
}

-(void) dealloc
{
    NSLog(@"DEALLOCING");
}

回答1:


I had the same issue as you, but I managed to fix the memory leak by calling this on viewDidDisappear:

self.avPlayer?.replaceCurrentItem(with: nil)



回答2:


The problem was with the idleTimer. When the invalidate method is called on the idleTimer, it doesn't synchronously invalidate the timer, instead, it waits for the next tick(not sure, but does wait for some time) before invalidating and releasing it.

Now, in the meanwhile, the idleTimer reference is being set to nil. On the next tick of the timer, the reference is lost and the memory is never released, and the references propagate all the way to the ViewController and none of its objects are released.



来源:https://stackoverflow.com/questions/24820725/memory-leak-in-ios-avplayer-is-never-deallocated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!