AVAudioPlayer - strange memory leak

102 views
Skip to first unread message

valvoline

unread,
Aug 13, 2009, 4:07:28 AM8/13/09
to iphonesdkd...@googlegroups.com
Hi all,

I'm experiencing a strange behavior with the AVAudioPlayer component.
Following many examples, i've set up my AppDelegate, in order to play
a loop sound during the main screen of my app. The code sounds like
the following:


/*sound loop stuff*/
NSString *soundFilePath = [[NSBundle mainBundle]
pathForResource:@"gong" ofType:@"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:fileURL error:nil];
[fileURL release];
self.loopSoundPlayer = newPlayer;
[newPlayer release];
[loopSoundPlayer setDelegate:self];
[loopSoundPlayer setNumberOfLoops:0];
[loopSoundPlayer prepareToPlay];

Also, the loopSoundPlayer is declared with a @property(nonatomic,
retain) constructor, in order to access the instance methods and
properties from the viewController class.

All seems to work fine, but if i check against memory leaks, i found a
spike in the very first part of the execution. Something like this:

208Bytes 0xd1f8a0 GeneralBlock-208
208Bytes 0xd1f970 GeneralBlock-208
208Bytes 0xd1fcf0 GeneralBlock-208
208Bytes 0xd1fe40 GeneralBlock-208
208Bytes 0xd1f700 GeneralBlock-208
32Bytes 0xd174e0 SegmentMachO
32Bytes 0xd1f6a0 SegmentMachO
32Bytes 0xd1f6e0 SegmentMachO
...
...

If i inspect every entry, using the details arrow button i found
*ONLY* audio related functions.

What's the problem? Is it my mistake? Is there a SDK bug?


best regards

--
gpg.0x1D67B4DD

amulter

unread,
Aug 14, 2009, 9:33:03 AM8/14/09
to iPhone SDK Development
just speculating here, but since the loopSoundPlayer is nil to start,
doesn't it makes sense that the line

self.loopSoundPlayer = newPlayer;

isn't actually calling the setter, and no retain is being performed?
why bother with the intermediary newPlayer at all? why not just call

loopSoundPlayer = [[AVAudioPlayer alloc] init... ]

???

Bess

unread,
Aug 15, 2009, 4:01:38 PM8/15/09
to iPhone SDK Development
Problems. Don't release the fileURL or newPlayer even before you play
the sound. You have to retain them when you play them. Release them
after playing. I would even release newPlayer outside the method for
repeated use.

/*sound loop stuff*/
NSString *soundFilePath = [[NSBundle mainBundle]
pathForResource:@"gong" ofType:@"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:fileURL error:nil];
[fileURL release];
self.loopSoundPlayer = newPlayer;
[newPlayer release];
[loopSoundPlayer setDelegate:self];
[loopSoundPlayer setNumberOfLoops:0];
[loopSoundPlayer prepareToPlay];

关戍

unread,
Aug 17, 2009, 4:11:45 AM8/17/09
to iphonesdkd...@googlegroups.com
your code is so strange...

Never release it as you will use it.

2009/8/16 Bess <bes...@gmail.com>:

Bess

unread,
Aug 18, 2009, 8:13:51 PM8/18/09
to iPhone SDK Development
Agree. Don't release it before you use it or you are using it
throughout the app

On Aug 17, 1:11 am, 关戍 <iamnota...@gmail.com> wrote:
> your code is so strange...
>
> Never release it as you will use it.
>
> 2009/8/16 Bess <bess...@gmail.com>:

shiva4u

unread,
Aug 25, 2009, 10:18:20 AM8/25/09
to iPhone SDK Development
Hi Bess,

I am also facing same memory leak problem.

its very strange, in Simulator i am getting GeneralBlock-240 and
SegmentMachO leaks. In Device i am getting GeneralBlock-3584 leak,
when i am using AVAudioPlayer.

If you have any solution for it, please let me know.

Thank you.
> > >> loopSoundPlayer = [[AVAudioPlayeralloc] init... ]

Agha Khan

unread,
Aug 26, 2009, 2:50:04 AM8/26/09
to iphonesdkd...@googlegroups.com
No solution yest. Let me know if you find one.
Best regards
Agha

shiva4u

unread,
Aug 28, 2009, 4:50:43 AM8/28/09
to iPhone SDK Development
Hi,

Some how i get rid of AVAudioPlayer leak in simulator

But still getting General Block-3584 leak.

Could any one help.

Thanks
shiva

On Aug 26, 11:50 am, Agha Khan <agha.shu...@gmail.com> wrote:
> No solution yest. Let me know if you find one.Best regards
> Agha

Bess

unread,
Aug 31, 2009, 9:43:27 PM8/31/09
to iPhone SDK Development
It makes sense when your simulator and device gives different leak.

I have done enough research on audio in both simulator and device.
Audio works very differently in device. So don't rely on simulator.
Always use device for code testing.

Rich

unread,
Sep 3, 2009, 11:00:50 AM9/3/09
to iPhone SDK Development
I was having the same trouble with AVAudioPlayer causing a leak in my
app. Tried everything to fix it and got so frustrated I wrote a bare
bones app with just a button that uses AVAudioPlayer to play a sound
and it STILL leaked. In my frustration I decided to see if Apple's
code was OK - I just ran Apple's sample code app "AddMusic" through
the leaks instrument on the device and it leaked in exactly the same
way. I've decided just to ignore it - as you probably already have.
Reply all
Reply to author
Forward
0 new messages