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

即使我做类似的事情:

-(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 时,我会立即从初始化中调用 Myclass 的释放

知道发生了什么吗?

4

1 回答 1

0

通常代表不保留,只分配。该代表应保留在其他地方。除其他外,这可以防止保留循环发生。

于 2009-10-09T17:00:13.927 回答