检测/修复内存泄漏

我已经完成了我的项目编码,但是当我在客户端提交我的源代码时,它已经过testing,然后检测到内存泄漏。 我已经在Instruments using Leaks进行了testing。 我遇到的问题是在我的AVPlayer和我的AVAudioPlayer和我的AppDelegate东西。 我应该find一个备用吗? 或者我的代码有问题?

这里是我的代码下面(我正在使用ARC的方式):

—> AppDelegate

 int main(int argc, char *argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([RootViewAppDelegate class])); } } 

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [window makeKeyAndVisible]; 

—> AVPlayer

 self.moviePlayer = [AVPlayer playerWithURL:[NSURL fileURLWithPath:moviePath]]; 

—> AVAudioPlayer

 NSString* resourcePath = [[NSBundle mainBundle] resourcePath]; resourcePath = [resourcePath stringByAppendingString:@"/Normal.wav"]; NSLog(@"Path to play: %@", resourcePath); NSError* err; //Initialize our player pointing to the path to our resource BGMplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:resourcePath] error:&err]; if( err ){ //bail! NSLog(@"Failed with reason: %@", [err localizedDescription]); } else{ //set our delegate and begin playback BGMplayer.delegate = self; [BGMplayer play]; BGMplayer.numberOfLoops = -1; BGMplayer.currentTime = 0; BGMplayer.volume = 1.0; } 

以下是我如何检测以上内容:

在这里输入图像说明


希望有人会帮助我,这是有点我第一次检测内存泄漏。 所以我希望你的指导。 非常感谢你。

首先你应该通过Shift + Command + B组合键分析你的代码,希望它能告诉你内存泄漏。

我的猜测是与

 //set our delegate and begin playback BGMplayer.delegate = self; 

AVPlayer保留了你的视图,而且由于你的视图还保留了AVPlayer,所以你有一个保留圈,可以防止两者都被弧释放。

我build议将代理设置为零,当它不再需要…也许通过覆盖在您的看法removeFromSuperview

将以下内容添加到您的视图中:

 - (void)removeFromSuperview { BGMplayer.delegate = nil; [super removeFromSuperview]; } 

看来你的一些实例variables没有被释放。 我认为,你的moviePlayer属性是保留AVPlayer对象,你不释放dealloc (也许一些其他的实例variables)。 希望这可以帮助。