AVAudioPlayer memory leak

青春壹個敷衍的年華 提交于 2020-01-09 07:18:32

问题


I'm stuck on some weird memory leak problem related to the AVAudioPlayer and I need help after trying everything that came to mind.

Here is the short description of the problem - code appears right after. I initialize my player and start to play the sound track in an endless loop (and endless loop or one time play did not change the problem). Several seconds after the music started, I switch to another sound track, hence I create a new player, initialize it, release the old one (which is playing) and then set the new one in place and play it.

At that point in time (right after I call the new Player - [Player play]) I get a memory leak (of 3.5Kb).

I tried the following:

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

  • Release the Player right after the play instruction - did not start playing

  • Release twice the old player - crash

  • Memory leak DOES NOT happen when I create and play the first Player!

Also, in the reference it does say that the 'play' is async and so probably it increases the ref count by 1, but in this case, why didn't [Player stop] help?

Thanks,

Here are some parts of the code about 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;

}


回答1:


Here is method to create AVAudioPlayer without causing memory leaks. See this page for explaination.

I have confirmed in my app that this removed my AVAudioPlayer leaks 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;
}



回答2:


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

eg.

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



回答3:


Your code looks OK to me as far as I've seen, so maybe there is code elsewhere which is causing the problem.

I will say that you're using a sort of odd idiom. Rather than retaining on create and releasing on set, I'd 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];
}

In this way, you only retain "player" objects when they actually come into your setAudioPlayer method, which might make it easier to track down.

Also, verify that it's actually an AVAudioPlayer object which is leaking. Instruments should be able to verify this for you.




回答4:


Try adding MediaPlayer.framework to your project



来源:https://stackoverflow.com/questions/1352588/avaudioplayer-memory-leak

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!