AudioToolBox leak in iOS6?

前端 未结 2 1915
忘掉有多难
忘掉有多难 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?

提交回复
热议问题