AvAudioPlayer将委托设置为nil会释放委托对象吗? [英] AvAudioPlayer setting delegate to nil releases the delegate object?

查看:76
本文介绍了AvAudioPlayer将委托设置为nil会释放委托对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@implementation MyClass

-(id) init
{
    NSString *path0 = [ [NSBundle mainBundle] pathForResource:@"myfile" ofType:@"m4a" ];
    mSound = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path0] error:NULL]; 
    mSound.delegate = self;  
}

-(void) release 
{

    mSound.delegate = nil;  //<- this line causes MyClass release function to be called recursively
   [ mSound release ];      //<- removing the line above makes this line do the same, i.e. calls myclass release recursively  
}

似乎释放AvAudioPlayer也会释放委托对象,在将其分配给委托时,我尝试手动调用keep on self,但这无济于事.

It seems that releasing AvAudioPlayer releases the delegate object as well, I tried to call retain on self manually when assigning it to the delegate but it didn't help.

即使我做类似的事情:

-(id) init
{
    NSString *path0 = [ [NSBundle mainBundle] pathForResource:@"myfile" ofType:@"m4a" ];
    mSound = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path0] error:NULL]; 
    mSound.delegate = self; 
    mSound.delegate = nil;  //<- (just for test), causes MyClass release to be invoked,    
}

当我将委托分配为nil时,我会立即从init释放Myclass

I get release of Myclass to be called right away from the init when I assign the delegate to nil

有什么想法吗?

推荐答案

通常,代表不保留,只能分配.该代表应保留在其他地方.除其他外,这使保留周期不会发生.

Typically delegates do not retain, only assign. The delegate should be retained elsewhere. Among other things this keeps retain cycles from occurring.

这篇关于AvAudioPlayer将委托设置为nil会释放委托对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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