【发布时间】:2013-02-10 04:54:15
【问题描述】:
这是我看到内存泄漏的代码(启用了 ARC)。
请帮我解决这个问题。
- (void) setMusic
{
/*
Initialise and set the music for the game.
*/
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"actionMusic" ofType:@"caf"]];
NSError *error = nil;
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (!error)
{
_audioPlayer.delegate = self;
if(_musicFlag)
{
[_audioPlayer play];
}
[_audioPlayer setNumberOfLoops:INT32_MAX];
}
error = nil;
url =nil;
url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"pew" ofType:@"wav"]];
_moveMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (!error)
{
_audioPlayer.delegate = self;
}
error = nil;
url =nil;
url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Won" ofType:@"wav"]];
_winningMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (!error)
{
_winningMusic.delegate = self;
[_winningMusic setNumberOfLoops:1];
}
}
【问题讨论】:
-
ARC 已启用 ....
-
注释掉内容,直到内存泄漏停止。发生内存泄漏的最后一行是原因。
-
什么告诉你有泄漏?报告哪条线路导致泄漏。您的属性是如何声明的?
-
你能分享一下静态分析器发现了什么,你在 Instruments 中发现了什么?
-
旁注:您所有的
if语句都不正确。如果方法的返回值为nil,则只应检查error。所以第一个应该是:if (_audioPlayer).
标签: ios objective-c memory-leaks ios6 avaudioplayer