Search code examples
objective-ccocoamemory-leaksios4avaudioplayer

Memory leak using AVAudioPlayer


I'm analyzing leaks in my Objective-C++ code using the instrumentation, and I'm getting this:

Leaked Object   Address Size    Responsible Library Responsible Frame
Malloc 32 Bytes,0x8135bc0   32 Bytes    AudioToolbox    SimAggregateDevice::SimAggregateDevice(__CFString const*, __CFString const*, long&)
__NSCFDictionary,0x8135be0  48 Bytes    AudioToolbox    CreateDictionaryForDevice(unsigned long)
Malloc 32 Bytes,0x8135c10   32 Bytes    AudioToolbox    CreateDictionaryForDevice(unsigned long)
Malloc 32 Bytes,0x8135c30   32 Bytes    AudioToolbox    CreateDictionaryForDevice(unsigned long)
Malloc 48 Bytes,0x8135c50   48 Bytes    AudioToolbox    SimAggregateDevice::SimAggregateDevice(__CFString const*, __CFString const*, long&)
__NSCFDictionary,0x813a820  48 Bytes    AudioToolbox    SimAggregateDevice::SimAggregateDevice(__CFString const*, __CFString const*, long&)
__NSCFArray,0x813a850   32 Bytes    AudioToolbox    SimAggregateDevice::SimAggregateDevice(__CFString const*, __CFString const*, long&)
__NSCFDictionary,0x813a870  48 Bytes    AudioToolbox    CreateDictionaryForDevice(unsigned long)
Malloc 32 Bytes,0x813a8a0   32 Bytes    AudioToolbox    SimAggregateDevice::SimAggregateDevice(__CFString const*, __CFString const*, long&)
Malloc 32 Bytes,0x813a8d0   32 Bytes    AudioToolbox    CreateDictionaryForDevice(unsigned long)

I assume this is related to the code I wrote to play a sound when my application starts. I do it this way: this method is invoked in applicationDidFinishLaunching::

- (void)playJingle {
    // This is a detached thread so I need a new pool.
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    // This audio player will be released in  audioPlayerDidFinishPlaying.
    NSString *newAudioFile = [[NSBundle mainBundle] pathForResource:@"jingle" ofType:@"m4a"];
    AVAudioPlayer *av = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:newAudioFile] error:NULL];
    [av setDelegate:self];
    [av play];

    // Release.
    [pool release];
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer*)av successfully:(BOOL)flag
{
    // Release the AVAudioPlayer.
    [av release];
}

in a background thread (invoked with performSelectorInBackground:withObject:). Am I missing something? Why do I have those leaks? Thanks!


Solution

  • Have you tried running the application on the device? AVAudioPlayer is prone to false positives with memory leak detection on the simulator...

    Here's a very similar question with a similar looking leak report