2011-09-07 200 views
3

我在我的iPhone应用程序中有以下代码,警告内存泄漏!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”

我对象的潜在泄漏尝试autorelease“moviePlayerController”然后我尝试释放它。这两种情况下内存泄漏都已解决,但视频无法在iPhone上播放!奇怪请帮忙。

+0

可能重复的[如何释放MPMoviePlayerController?](http://stackoverflow.com/questions/695307/how-to-release-mpmovieplayercontroller) – albertamg

回答

4

警告是正确的:您正在泄漏MPMoviePlayerController实例。但是正如你发现的那样,如果不保持控制器周围的环境,你就无法有效地使用视图。

的解决方案是将MPMoviePlayerController存储到你的班上伊娃/属性,然后释放它,当你与它的视图中完成(例如,在viewDidUnloaddealloc)。

+0

这确实奏效,谢谢! – HardCode

0

尝试在你的头文件

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

然后在.m文件@synthesize moviePlayerController;

然后尝试self.moviePlayerController = [[[MPMoviePlayerController alloc] initWithContentURL:fileURL] autorelease];

最后加入self.moviePlayerController = nil[moviePlayerController release]viewDidUnloaddealloc