iPhone开发中使用AVAudioPlayer出现内存泄漏的解决办法
?
-(id)init{ if (self = [super init]) { NSString *path = [[NSBundle mainBundle] pathForResource:@"GameOver" ofType:@"mp3"]; NSError *error = nil; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error]; audioPlayer.delegate = self; [audioPlayer prepareToPlay]; audioPlayer.numberOfLoops = -1; [audioPlayer play]; } return self;}?
-(void)dealloc{ if (audioPlayer && [audioPlayer isPlaying]) { [audioPlayer stop]; } [audioPlayer release]; audioPlayer = nil; [super dealloc];}?
-[AVAudioPlayer initWithData:error:]
?and-[AVAudioPlayer initWithContentsOfURL:error:]
AVAudioPlayer
?instance retains the passed in?NSData
. In the second, the passed in?NSURL
?is retained:-(void)dealloc{ [audioPlayer.url release]; if (audioPlayer && [audioPlayer isPlaying]) { [audioPlayer stop]; } [audioPlayer release]; audioPlayer = nil; [super dealloc];}?
?