I have the following code in my iPhone application, warning memory leak!
This is my code
-(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];
}
This is the error message I’m getting:
Potential leak of an object allocated on line 37 and stored into ‘moviePlayerController’
I did try to autorelease “moviePlayerController” and then I try to release it. Both cases memory leak was solved but the video didn’t play on the iPhone! Strange please help.
The warning is correct: you are leaking the
MPMoviePlayerControllerinstance. But as you’ve discovered, you can’t effectively use the view without keeping the controller around.The solution is to store the
MPMoviePlayerControllerinto an ivar/property in your class, and then release it when you are done with its view (e.g. inviewDidUnloadanddealloc).