AudioToolBox leak in iOS6?

前端 未结 2 1914
忘掉有多难
忘掉有多难 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:17

    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?

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题