0
votes
@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?

1
While this is not the answer to your problem, you mixed up release and dealloc. Releasing instance variables should be done in dealloc. - Nikolai Ruhe

1 Answers

0
votes

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