When I use AudioToolBox for playing music, memory leaks heavily.
AVAudioPlayer *newMusicPlayer = [[AVAudioPlayer alloc] initWithData:data error:&error];
Maybe you did better than me and I haven't the XCode in my front, but :
object created, reference created:
AVAudioPlayer *newMusicPlayer = [[AVAudioPlayer alloc] initWithData:data error:&error];
it is assignment and not allocation, no retain:
_musicPlayer = newMusicPlayer;
out of function playMusicWithMusicData
it would deallocate the newMusicPlayer
and it would crash my code in other function when I use the _musicPlayer
. The analyzer tool nagged so much as I made a retain, maybe a release too.
if you are using ios4, than isn't ARC this project.
in if branch:
[_musicPlayer stop];
_musicPlayer = nil;
I would check the _musicPlayer
to not be nil, but that will not solve your problems.
No clue where it could be, if not in ios and probably there it will be:
Did solve if you change the delegate instead of self.
_musicPlayer.delegate = self;
to any other class?
I'm playing audio from a file using AVAudioPlayer
, and found that the iOS simulator consistently leaks memory, but the device doesn't. This was verified using Instruments. This is ARC code.
My player is declared like this:
@property (nonatomic, retain) AVAudioPlayer *numberPlayer;
and synthesized like this:
@synthesize numberPlayer = _numberPlayer;
Since I need to play several different sounds, and AVAudioPlayer
cannot be reset to play a different audio file after its creation, I'm creating a new player every time, like this:
NSString *audioFilePathName = [NSString stringWithFormat:@"Voices/%@/%03i.m4a", self.voiceName, self.theNumber];
NSURL *url = [NSURL fileURLWithPath:BUNDLE_FULL_PATH(audioFilePathName)];
self.numberPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
self.numberPlayer.numberOfLoops = 0;
[self.numberPlayer setCurrentTime:0.0];
[self.numberPlayer setDelegate:self];
[self.numberPlayer prepareToPlay];
[self.numberPlayer play];
In my delegate I set the player to nil
when it has finished playing:
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
if (player == self.numberPlayer) {
_numberPlayer = nil;
}
}
In the simulator, both AudioSessionDevice
and UISoundNewDevice
leak memory. However, the biggest leaker is actually NSURL
. None of these leaks occur on the device. This behavior has not changed in iOS 6, but I do have my project deployment set to 5.0, should that make any difference.
See also https://stackoverflow.com/questions/3433487/avaudioplayer-leak-in-simulator and memory leak in AudioToolbox library AVAudioPlayer.