iOS中的内存泄漏,AVPlayer永远不会被释放 [英] Memory leak in iOS, AVPlayer is never deallocated

查看:1509
本文介绍了iOS中的内存泄漏,AVPlayer永远不会被释放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用AVPlayerDemo样品从。



//编辑

   - (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);
}


解决方案

问题在于idleTimer 。当在idleTimer上调用invalidate方法时,它不会同步使计时器失效,而是在无效并释放它之前等待下一个滴答(不确定,但确实等待一段时间)。



现在,同时,idleTimer引用被设置为nil。在计时器的下一个滴答声中,引用将丢失并且内存永远不会释放,并且引用一直传播到ViewController,并且不会释放任何对象。


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

解决方案

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.

这篇关于iOS中的内存泄漏,AVPlayer永远不会被释放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆