AvAudioPlayer设置委托给nil释放委托对象?

时间:2009-10-09 16:54:49

标签: iphone delegates avaudioplayer

@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也释放了委托对象,我试图在将其分配给委托时手动调用retain,但它没有帮助。

即使我做了类似的事情:

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

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

通常,代表不会保留,只能分配。代表应保留在其他地方。除此之外,这可以保持周期不发生。