I posted a much longer question a few minutes ago, and as it usually goes as soon as I posted it I realized what was going on, so I deleted it since most of the post was irrelevant. Then I went back to Google.
Turns out I'm having almost the same exact problem as described in this post, unanswered from June. http://www.iphonedevsdk.com/forum/iphone-sdk-development/20975-avaudioplayer-nsurl-memory-management.html
In summary: I'm using AVAudioPlayer and releasing it with the audioPlayerDidFinishPlaying:successfully: delegate method. After I initialize the player, its associated NSURL object needs to be freed, otherwise it leaks. But when I release it after initializing the player, it crashes since it has already been freed. The weird thing is that it doesn't always crash the first time, most of the time it crashes after the second sound has been played. Sometimes (rarely) it will allow a handful or players to be alloc/released (I reuse the pointers after freeing) before crashing. Any help?
Code snippet: (soundKeyUp is an AVAudioPlayer* class variable, hence no declaration here)
NSString *soundKeyUpPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"soundKeyUp%d", key.tag % 5] ofType:@"wav"];
NSURL *soundKeyUpURL = [[NSURL alloc] initFileURLWithPath:soundKeyUpPath];
soundKeyUp = [[AVAudioPlayer alloc] initWithContentsOfURL:soundKeyUpURL error:nil];
if(soundKeyUp) {
[soundKeyUp setDelegate:self];
[soundKeyUp play];
}
else {
[soundKeyUp release];
soundKeyUp = nil;
}
[soundKeyUpPath release];
[soundKeyUpURL release];
Response to Steve Riggins: The trick of it is that it doesn't crash at the same time every time, as mentioned. It almost always properly releases the first time (or at least, it doesn't crash or leak) but usually after the second time I allocate/release the player and URL, it crashes on releasing the URL. Sometimes, it goes 3, 4, 5+ times before crashing, but it always does.