EDIT: Updated my device (and deployment target) from iOS 4.3.5 to iOS 5.1.1 and still has the same issue
Having a problem releasing a class member AVAudioPlayer to play another sound after playing a sound.
This only seems to be a problem on one of my devices, a 3rd generation iPod Touch running iOS 4.3.5/5.1.1.
My 4th and 5th generation devices running iOS 6 run just fine.
;
audio = nil;
@try
{
audio = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if ((audio != nil) && (!error))
{
audio.delegate = (id)self;
audio.volume = gVolume/10;
;
}
else
{
;
audio = nil;
}
}
@catch (NSException *exception)
{
;
audio = nil;
}
First time through it plays just fine, but when this code executes a second time to play another sound, it gives an EXC_BAD_ACCESS error, which does not happen on my 4th/5th generation devices.
I purposely delay the release until I need to play another sound in order to give the play time to perform.
Tried working with AVAudioPlayerDelegate but that didn’t solve the problem, nor does calling:
;
I do initialize audio to nil in viewDidLoad, and clean up in viewDidUnload and dealloc.
Was having a lot of memory leak issues but this version of the code seemed to have solved that.
Thanks to the comment above, and the failure of all my other ideas, I went ahead and took advantage of ARC.
I didn’t want to refactor my entire project to start using ARC, so I rewrote a new class to handle all of my audio needs and set some compiler flags (-fobjc-arc) to enable ARC just for that file.
Not exactly sure what I was doing wrong before, but the system seems to be much better at managing memory than I am, and I’m happy to let it solve all my problems.
EDIT 2:
Apparently Apple did finally fix their memory leak so I went back to my ARC solution which works fine for both iOS 5 and iOS 6.
EDIT:
I may have been a little premature.
While implementing ARC for this was helpful in general, this specific problem goes deeper.
Apparently, there is a leak in Core Foundation for iOS 6.
Leak from NSURL and AVAudioPlayer using ARC
Memory Leak – NSString & NSURL
Using ARC, it works fine on my Gen 3 iOS 5 device, but leaks significantly for Gen 4/5 iOS 6.
Without ARC, Gen 4/5 iOS 6 works fine, but Gen 3 iOS 5 crashes.
I ended up making two versions of my new class, one using ARC and one not, and checking the system version at runtime [[UIDevice currentDevice] systemVersion] to determine which version to use.
It may be a little clumsy, but does allow me to support the Gen 3 devices. If Apple ever fixes their problem I can return to a single version at that time.