iOS中的内存泄漏,AVPlayer从未释放

Faraz Hassan |

我使用了Apple文档中的AVPlayerDemo示例,并在其上面编写了自己的UI来播放从UITableViewController中选择的视频。现在,问题是我找不到的某个地方存在内存泄漏。问题是AVPlayer对象没有被取消分配,我猜这是因为每次按下后退按钮并选择要播放的新视频时,该应用程序消耗的总内存就会大幅增加,如下所示:

第一次播放视频时,内存使用量为36.6MB

第一次播放视频时,内存使用量为36.6MB,现在是第二次:

在这里它已经跳到58.2MB

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");
}
Faraz Hassan

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.

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章