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 を解放すると、デリゲート オブジェクトも解放されるようです。デリゲートに割り当てるときに、retain on 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 に答える