AudioToolBox leak in iOS6?

前端 未结 2 1913
忘掉有多难
忘掉有多难 2021-01-06 10:58

When I use AudioToolBox for playing music, memory leaks heavily.

AVAudioPlayer *newMusicPlayer = [[AVAudioPlayer alloc] initWithData:data error:&error];
         


        
2条回答
  •  别那么骄傲
    2021-01-06 11:27

    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.

提交回复
热议问题