AvAudioPlayer设置委托以释放委托对象?

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,但它没有帮助。AvAudioPlayer设置委托以释放委托对象?

即使我做这样的事情:

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

我得到MYCLASS的释放时我分配委托零

任何想法怎么回事从初始化马上叫什么名字?

+0

虽然这不是你问题的答案,但你混淆了'release'和'dealloc'。释放实例变量应该在'dealloc'中完成。 – 2009-10-09 17:49:21

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

+0

所以这意味着我的代码应该可以工作,但它不? – Auday 2009-10-09 17:03:03