MPMoviePlayerViewController iPad 内存泄漏

发布于 2024-09-26 12:30:31 字数 1581 浏览 6 评论 0原文

我的电影播放器​​仅在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

就像说晚安 2024-10-03 12:30:31

删除观察者时,MPMoviePlayer 实例已通过自动释放保留。用自动释放包装删除观察者代码将使保留计数达到预期。

像这样:

// 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;

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;
旧人九事 2024-10-03 12:30:31

您忘记在 init 附近释放 moviePlayer。你可以这样做:

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];

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];
眼泪都笑了 2024-10-03 12:30:31

我有同样的问题。为了在没有内存泄漏的情况下停止它,我必须快进到最后几秒钟,以便它可以自行完成。

但后来我发现这个漏洞只发生在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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文