开发者

MPMoviePlayerViewController iPad Memory Leak

My movie player leaks memory only on the iPad and only when the "Done" button is clicked. If the movie plays to completion then it cleans itself up properly. Here is the play code:

mViewPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[self movieURL:@"mymovie"]];
[self.parentViewController presentModalViewController:mViewPlayer animated:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitedFullscreen:) name:MPMoviePlayerPlaybackDidFinishNotification object:[mViewPlayer moviePlayer]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitedFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:[mViewPlayer moviePlayer]];

And here is the cleanup code:

- (void)exitedFullscreen:(NSNotification*)aNotification 
{
 MPMoviePlayerController *player = [aNotification object];
 [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];
 [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:player];

 [self.parentViewController dismissModalViewControllerAnimated:YES];
 NSLog(@"retainCount theMovie: %i", [mViewPlayer retainCount]); 
 player.initialPlaybackTime = -1;
 [player pause];
 [player stop];
 NSLog(@"retainCount theMovie: %i", [mViewPlayer retainCount]); 
 [player release];
 player = nil;
// [mViewPlayer release];
 mViewPlayer = nil;
}

retainCount is 3 both times it is printed above and is the same when the movie completes normally or when the "Done" button is clicked.

I have trie开发者_Go百科d using MPMoviePlayerController as well with the same results. I have tried using prepared play and 10 different methods to call the MPMoviePlayer*Controller, but it always leaks when I click the Done button.

Any help would be greatly appreciated. Thanks.


When removing observer the MPMoviePlayer instance has been retained with an autorelease. Wrapping the remove observer code with an autorelease will make the retainCount be as expected.

Like this:

// remove all observers
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // following messages adds to autorelease pool
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:player];
[pool drain], pool = nil;


You forgot to release the moviePlayer close to init. You could do it like this:

mViewPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[self movieURL:@"mymovie"]];
[self.parentViewController presentModalViewController:mViewPlayer animated:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitedFullscreen:) name:MPMoviePlayerPlaybackDidFinishNotification object:[mViewPlayer moviePlayer]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitedFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:[mViewPlayer moviePlayer]];
[mViewPlayer release];


I have the same problem. To stop it without memory leak, I have to fast forward to the last few seconds so it can complete by itself.

But later i found out that this leak is only on the iPad Simulator. It is fine if you run it on device.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜