读书人

iPhone开发中使用AVAudioPlayer出现内

发布时间: 2012-12-31 11:57:52 作者: rapoo

iPhone开发中使用AVAudioPlayer出现内存泄漏的解决办法

?


最近在使用AVAudioPlayer播放音频时,发现有内存泄漏的现象,我的代码如下: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];}
?
跟踪Instruments工具中的泄漏情况,发现都是在NSURL或NSData泄漏了,在stackoverflow发现有人这么说(帖子:http://stackoverflow.com/questions/12498015/leak-from-nsurl-and-avaudioplayer-using-arc):

Looks to be a leak in Apple's code... I tried using both
读书人网 >Iphone

热点推荐