@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 的释放
知道发生了什么吗?