AVAudioPlayer memory leak

I am stuck in some strange memory leak problem related to AVAudioPlayer and I need help after you have tried everything that comes to mind.

Here is a brief description of the problem - the code appears immediately after. I initialize my player and start playing the soundtrack in an infinite loop (and an infinite loop or single play did not change the problem). A few seconds after the start of the music, I switched to another sound track, so I create a new player, initialize him, release the old one (which plays), and then set the new one in its place and play it.

At this point in time (right after calling a new player - [Player play]) I get a memory leak (3.5Kb).

I tried the following:

  • Stop the old player and then release it - no effect

  • Release the player immediately after the playback instruction - did not start playback

  • Issue of twice old player - accident

  • A memory leak does NOT occur when I create and play the first player!

In addition, in the link, he says that “play” is asynchronous, and therefore, probably, it increases the number of links by 1, but in this case why [Player stop] does not help?

Thanks,

Here are some parts of the code on how I use it:

- (void) loadAndActivateAudioFunction { NSBundle *mainBundle = [NSBundle mainBundle]; NSError *error; NSURL *audioURL = [NSURL fileURLWithPath:[mainBundle pathForResource: Name ofType: Type]]; AVAudioPlayer *player = [(AVAudioPlayer*) [AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error]; if (!player) { DebugLog(@"Audio Load Error: no Player: %@", [error localizedDescription]); DuringAudioPrep = false; return; } [self lock]; [self setAudioPlayer: player]; [self ActivateAudioFunction]; [self unlock]; 

}

 - (void) setAudioPlayer : (AVAudioPlayer *) player { if (Player) { if ([Player isPlaying] || Repeat) // The indication was off??? [Player stop]; [Player release]; } Player = player; 

}

 - (void) ActivateAudioFunction { [Player setVolume: Volume]; [Player setNumberOfLoops: Repeat]; [Player play]; DuringAudioPrep = false; 

}

+6
memory memory-leaks xcode avaudioplayer
source share
4 answers

Here is a way to create AVAudioPlayer without causing a memory leak. See this page for an explanation .

I confirmed in my application that this removed my AVAudioPlayer leak 100%.

 - (AVAudioPlayer *)audioPlayerWithContentsOfFile:(NSString *)path { NSData *audioData = [NSData dataWithContentsOfFile:path]; AVAudioPlayer *player = [AVAudioPlayer alloc]; if([player initWithData:audioData error:NULL]) { [player autorelease]; } else { [player release]; player = nil; } return player; } 
+7
source share

Implement the AVAudioPlayerDelegate protocol and its audioPlayerDidFinishPlaying method: successful: then release the audio player object

eg.

 - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { [player release]; // releases the player object } 
+2
source share

Your code looks ok as far as I saw, so maybe there is code in another place that is causing the problem.

I will say that you are using a kind of strange idiom. Instead of saving when creating and releasing on a set, I would do something like this:

  // new players will always be created autoreleased. AVAudioPlayer *player = [[(AVAudioPlayer*) [AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error] autorelease]; - (void) setAudioPlayer : (AVAudioPlayer *) player { if (Player) { if ([Player isPlaying] || Repeat) // The indication was off??? [Player stop]; [Player release]; } Player = [player retain]; } 

This way, you only save player objects when they really go into your setAudioPlayer method, which can make tracking easier.

Also, make sure that it is actually an AVAudioPlayer object that is leaking. Tools should be able to verify this for you.

0
source share

Try adding MediaPlayer.framework to your project.

-2
source share

All Articles