HardCode
HardCode

Reputation: 99

iPhone Memory leak issue

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.

Upvotes: 3

Views: 464

Answers (2)

Jiho Kang
Jiho Kang

Reputation: 2492

try adding MPMoviePlayerController *moviePlayerController in your header file

then @property (nonatomic, retain) MPMoviePlayerController *moviePlayerController;

then in your .m file @synthesize moviePlayerController;

then try self.moviePlayerController = [[[MPMoviePlayerController alloc] initWithContentURL:fileURL] autorelease];

lastly add self.moviePlayerController = nil and [moviePlayerController release] to your viewDidUnload and dealloc.

Upvotes: 0

Anomie
Anomie

Reputation: 94794

The warning is correct: you are leaking the MPMoviePlayerController instance. But as you've discovered, you can't effectively use the view without keeping the controller around.

The solution is to store the MPMoviePlayerController into an ivar/property in your class, and then release it when you are done with its view (e.g. in viewDidUnload and dealloc).

Upvotes: 4

Related Questions