Q:

AVAudioPlayer Memory Management

I'm trying to understand some memory issues concerning AVAudioPlayer and AVAudioRecorder.
I create an AVAudioRecorder:
-(void)loadAudioForPage {
AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:audioFileURL settings:recordSettings error:&err];

self.audioRecorder = recorder;
[recorder release];

}
this method will be called several times during the life of the controller it resides in, and each time the property audioFileURL will be different.
I've assigned recorder it to an instance variable of my view controller, the property retains it, and so I release.
Now, all might be well if I simply released audioRecorder in dealloc, except I need to assign another instance to that property before dealloc is going to be called.
So, my thought was to put it in the delegate method:
-(void) audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag {

[audioRecorder release];
self.audioRecorder = nil;

}

This, however, crashes. Can someone explain to me why? Is an AVAudioRecorder somehow autoreleased even though I alloc'd it? How is it that I'm retaining it twice, and the second time I release it, it crashes (EXEC_BAD_ACCESS)?

A:

When you use generated property accessors, they manage all the memory for you. So, if you set the property to nil, and it previously had another value, it will release the old value and then set the current value to nil.
So, in your code, this would result in releasing the property's previous object twice (once directly by you and again by the property accessor when setting it to nil). You must either manage the property directly or let the accessor manage the property.
So, either:
[audioRecorder release];
audioRecorder = nil;

or:
[self setAudioRecorder:nil];

Since you're using properties and generated accessors, you should probably just stick with that and rewrite your delegate method:
-(void) audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag
{
  [self setAudioRecorder:nil];
}

The only place your might actually directly release the property is in the dealloc method.

