views:

358

answers:

1
@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  
}

It seems that releasing AvAudioPlayer releases the delegate object as well, I tried to call retain on self manually when assigning it to the delegate but it didn't help.

even if I do something like:

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

I get release of Myclass to be called right away from the init when I assign the delegate to nil

Any idea whats going on?

A: 

Typically delegates do not retain, only assign. The delegate should be retained elsewhere. Among other things this keeps retain cycles from occurring.

zaph
So that implies that my code above should work, but it doesn't ?