1

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 release];
audio = nil;
@try
{
    audio = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    if ((audio != nil) && (!error))
    {
        audio.delegate = (id)self;
        audio.volume = gVolume/10;
        [audio play];
    }
    else
    {
        [audio release];
        audio = nil;
    }
}
@catch (NSException *exception)
{
    [audio release];
    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:

[audio prepareToPlay];

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.

GRW
  • 605
  • 12
  • 27
  • Why not turn on ARC and make life easier for you? If you can't turn on ARC, could you run this in Xcode Instruments? – Michael Dautermann Jan 27 '13 at 05:32
  • Mostly because of my ignorance. Not exactly sure how to go about incorporating ARC into an existing app, but it is definitely on the short list of things to look into. Thanks. – GRW Jan 27 '13 at 17:45

1 Answers1

0

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.

Community
  • 1
  • 1
GRW
  • 605
  • 12
  • 27