MPMoviePlayerViewController iPad 内存泄漏
我的电影播放器仅在 iPad 上且仅当单击“完成”按钮时才会泄漏内存。如果电影播放完毕,它就会正确地自行清理。这是播放代码:
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]];
这是清理代码:
- (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都是3,并且当电影正常完成或单击“完成”按钮时是相同的。
我也尝试过使用 MPMoviePlayerController 并获得相同的结果。我尝试过使用准备好的播放和 10 种不同的方法来调用 MPMoviePlayer*Controller,但当我单击“完成”按钮时它总是泄漏。
任何帮助将不胜感激。谢谢。
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 tried 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
删除观察者时,MPMoviePlayer 实例已通过自动释放保留。用自动释放包装删除观察者代码将使保留计数达到预期。
像这样:
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:
您忘记在 init 附近释放 moviePlayer。你可以这样做:
You forgot to release the moviePlayer close to init. You could do it like this:
我有同样的问题。为了在没有内存泄漏的情况下停止它,我必须快进到最后几秒钟,以便它可以自行完成。
但后来我发现这个漏洞只发生在iPad模拟器上。如果你在设备上运行它就可以了。
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.