AvAudioPlayer setting delegate to nil releases the delegate object?

送分小仙女□ 提交于 2019-12-12 01:15:17

问题


@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  
}

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.

even if I do something like:

-(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,    
}

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

Any idea whats going on?


回答1:


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



来源:https://stackoverflow.com/questions/1544936/avaudioplayer-setting-delegate-to-nil-releases-the-delegate-object

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