【问题标题】:iPhone Memory leak issueiPhone内存泄漏问题
【发布时间】:2011-11-12 06:12:09
【问题描述】:

我的 iPhone 应用程序中有以下代码,警告内存泄漏!

这是我的代码

-(IBAction)playVideo:(id)sender  {  
     NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"test" 
                                                              ofType:@"mov"];  
     NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];  
     MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
    [moviePlayerController.view setFrame:CGRectMake(38, 100, 250, 163)];  
    [self.view addSubview:moviePlayerController.view];  
    moviePlayerController.fullscreen = YES;  
    [moviePlayerController play];  
} 

这是我收到的错误消息: 在第 37 行分配并存储到“moviePlayerController”中的对象的潜在泄漏

我确实尝试过自动释放“moviePlayerController”,然后尝试释放它。两种情况都解决了内存泄漏问题,但视频无法在 iPhone 上播放!奇怪请帮忙。

【问题讨论】:

标签: iphone objective-c memory-leaks


【解决方案1】:

警告是正确的:您正在泄漏MPMoviePlayerController 实例。但正如您所发现的,如果不保留控制器,您将无法有效地使用视图。

解决方案是将MPMoviePlayerController 存储到类中的 ivar/property 中,然后在完成其视图时释放它(例如在 viewDidUnload 和 dealloc 中)。

【讨论】:

    【解决方案2】:

    尝试在头文件中添加MPMoviePlayerController *moviePlayerController

    然后@property (nonatomic, retain) MPMoviePlayerController *moviePlayerController;

    然后在你的 .m 文件中@synthesize moviePlayerController;

    那就试试self.moviePlayerController = [[[MPMoviePlayerController alloc] initWithContentURL:fileURL] autorelease];

    最后将self.moviePlayerController = nil 和[moviePlayerController release] 添加到您的viewDidUnload 和dealloc。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-29
      • 2011-07-29
      • 1970-01-01
      相关资源
      最近更新 更多