MPMoviePlayerController 仍然泄漏

发布于 2024-09-12 13:25:00 字数 634 浏览 2 评论 0原文

我释放了 MPMoviePlayerController,但内存分配和活动对象仍然高于对象分配之前。但是,如果我重新分配该对象,它不会泄漏更多。 我的应用程序实际上使用了大量媒体文件,并且内存消耗很高。我想完全释放不需要的内存以避免内存警告。

电影播放器​​释放:

        player.initialPlaybackTime = -1;
        [player.view removeFromSuperview];
        [player stop];
        [player release];

电影播放器​​分配:

    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"video0_hd.mov" ofType:nil]];
    player = [[MPMoviePlayerController alloc] initWithContentURL:url];
    player.view.frame = placeholder.frame;
    [self.view addSubview:player.view];
    [player play];

I release the MPMoviePlayerController but the memory allocation and the living objects are still higher than before the object allocation. However if I reallocate the object it doesn't leak more.
My application actually uses a lot of media files and the memory consumption is high. I would like to free up completely the unneeded memory to avoid memory warnings.

Movie player release:

        player.initialPlaybackTime = -1;
        [player.view removeFromSuperview];
        [player stop];
        [player release];

Movie player alloc:

    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"video0_hd.mov" ofType:nil]];
    player = [[MPMoviePlayerController alloc] initWithContentURL:url];
    player.view.frame = placeholder.frame;
    [self.view addSubview:player.view];
    [player play];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

江心雾 2024-09-19 13:25:01

我也有这个问题。

iPad 用于预加载视频流的缓存并未完全清空。
所以每次,这个带有视频播放器的页面都会被清理并释放,清理后分配的内存仍然包含缓存的内存。对于大视频,这可能高达 50 MB。

这实际上不是内存泄漏:

如果再次打开页面,则重新分配缓存。但仍然令人沮丧,因为您想要一个干净的退出情况,这意味着当该页面被离开并清理时,该页面使用的所有内存都应该被释放,包括用于缓存视频流的内存......!

经过一番认真的调整后,这一系列命令似乎可以完成这项工作:

=======================

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:myMoviePlayer];        

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerDidExitFullscreenNotification
                                                  object:myMoviePlayer];        

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerLoadStateDidChangeNotification
                                                  object:myMoviePlayer];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMovieDurationAvailableNotification
                                                  object:myMoviePlayer];        
    [myMoviePlayer pause];
    myMoviePlayer.initialPlaybackTime = -1;
    [myMoviePlayer stop];
    myMoviePlayer.initialPlaybackTime = -1;
    [myMoviePlayer.view removeFromSuperview];
    [myMoviePlayer release];

============= ====================

步骤:

1 - 删除您用于电影播放器​​的所有通知程序。

2 - 暂停电影

3 - 设置播放时间开始

4 - 停止电影

5 - 再次设置播放时间开始

6 - 现在删除电影视图

7 - 最后释放电影播放器

​​ 在我的情况下也出现了视频我的 iPad(操作系统 4.2)上正在释放缓存内存
并留下干净的分配内存空间,等于打开视频播放器页面之前的大小。所以同样的进入和退出内存。

希望这有帮助……

I also had this issue.

The cache used by the iPad for preloading the video stream, was not emptied completely.
So each time, this page with the video player was cleaned up and released, the allocated memory after cleanup still contained the cached memory. For big video's, this could be up to 50 MB.

This is actually not a memory leak:

If the page was opened again, the cache was re-allocated. But still frustating as you want a clean exit situation, meaning when this page is left and cleaned up, all memory used by this page should be released, including the memory used for caching the video stream....!

After some serious tweeking, this sequence of commands seems to do the job:

======================

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:myMoviePlayer];        

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerDidExitFullscreenNotification
                                                  object:myMoviePlayer];        

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerLoadStateDidChangeNotification
                                                  object:myMoviePlayer];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMovieDurationAvailableNotification
                                                  object:myMoviePlayer];        
    [myMoviePlayer pause];
    myMoviePlayer.initialPlaybackTime = -1;
    [myMoviePlayer stop];
    myMoviePlayer.initialPlaybackTime = -1;
    [myMoviePlayer.view removeFromSuperview];
    [myMoviePlayer release];

=================================

In steps:

1 - REMOVE all notifiers you are using for your movie player.

2 - Pause the movie

3 - set the Playback time to start

4 - stop the movie

5 - set the Playback time again to start

6 - now remove the movie View

7 - and finally release the movie player

Resulting in my case in also the video cache memory being released on my iPad (OS 4.2.)
and leaving a clean allocated memory situation, equal to the size before the page with the video player was openen. So same enter and exit memory.

Hope this helps......

你如我软肋 2024-09-19 13:25:01

您是否尝试过构建和分析(构建>构建和分析),这可以向您显示内存泄漏的确切行(如果有)。

现在,我感觉问题出在您定义 NSURL 的位置。

Have you tried Build and Analyze (Build>Build and Analyze), this could show you the exact line memory is leaking (if any).

Right now, I've got a feeling the problem is where you define the NSURL.

逆光飞翔i 2024-09-19 13:25:01

您是在设备上还是在模拟器上运行此代码?模拟器给出了一堆错误的泄漏(例如在 AudioToolbox、CoreVideo 等中)。此外,模拟器似乎缓存了整个视频,并且没有正确释放它,而设备仅缓冲沿途所需的内容。

我用 mp4 视频在模拟器上测试了你的代码,得到了与你所说的类似的结果(每次播放视频时有 10 个活动对象,没有一个死亡......分配了 20mb,甚至在释放后还剩下 5mb)。活动对象和内存分配将在模拟器上不断增长。

然而,在我的 iPhone(带有 20mb 视频)上,它只为应用程序分配了 900kb 的内存总量,在启动/停止/释放视频时没有明显的变化。我测试了 10 次(启动/停止/释放),它始终保持在 900kb 左右。

看起来又是一次你不能相信模拟器的情况了。

我正在测试的代码:

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"SomeMovieFile" ofType:@"mp4"]];

MPMoviePlayerController *newPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];

self.player = newPlayer;  
[newPlayer release];

[self.view addSubview:player.view];  // for my example, I didn't set the frame location, but no difference that would do
[player play];

然后在另一个按钮中我停止了它并释放了播放器:

[player.view removeFromSuperview];    
player.initialPlaybackTime = -1;
[player stop];
self.player = nil;  // this is just a habit of mine.. calling stop should unload the buffers though
[player release];

Are you running this code on a device or on the simulator? The simulator gives a bunch of false leaks (such as in AudioToolbox, CoreVideo, etc.). Also, the simulator seems to cache the entire video and not release it properly whereas the device buffers only what it needs along the way.

I tested your code on the simulator with an mp4 video and I had similar results to what you were saying (10 live objects each time a video was played, with none dying... 20mb allocated, and 5mb left even after releasing). The living objects and memory allocation would keep growing and growing on the simulator.

However, on my iPhone (with a 20mb video) it only allocated 900kb of memory total for the app, with no appreciable change when starting/stopping/releasing the video. It always stayed right around 900kb for the 10 times I tested it (starting/stopping/releasing).

Looks like just another time you can't trust the simulator.

The code I was testing with:

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"SomeMovieFile" ofType:@"mp4"]];

MPMoviePlayerController *newPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];

self.player = newPlayer;  
[newPlayer release];

[self.view addSubview:player.view];  // for my example, I didn't set the frame location, but no difference that would do
[player play];

Then in another button I stopped it and released the player:

[player.view removeFromSuperview];    
player.initialPlaybackTime = -1;
[player stop];
self.player = nil;  // this is just a habit of mine.. calling stop should unload the buffers though
[player release];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文