views:

335

answers:

2

I release the MPMoviePlayerController but the memory allocation and the living objects are sill higher than before the object allocation. However if i reallocate the object it doesn't leak more. My application actually uses alot of media files and the memory consumption it's 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];
A: 

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.

pop850
It's an autorelease object, the problem it's not there, for sure.
Cesar
Agreed... you only release what you specifically `alloc` / `init` ..everything else is not your responsibility, especially if it's a convenience class. Apple autoreleases those
iWasRobbed
A: 

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];
iWasRobbed
You should actually avoid the "player=nil; [player release];" because you are technically releasing "nil", *not* the allocated player, which could potentially cause a leak.
pop850
Actually, setting the object to nil will release the old value of the object and then set the object value to nil. Just like with any other object though, you still have to release anything you explicitly `alloc` / `init`.. I think you are getting confused with only setting an object to nil and then not releasing it. In that scenario, you are leaking the object since you still have a pointer to some object in memory that never gets deallocated.
iWasRobbed
Acutally this code it's tested on an iPad, and the memory allocation has never been release completely.Anyway, if you tested the code you posted and you din't had any memory leaks it's courious, You set to nil the pointer before release the object.
Cesar
Hmm.. I'm testing on an iPhone 4. I've heard of issues with iOS 3.2 and having to call `[player pause];` prior to stopping the player. Since I don't have an iPad, I can't be sure if that'd help you or not. Like I said, calling `[player stop];` is supposed to clear the buffers of data, but I just have a habit of setting to `nil` just in case. It should work either way. Also, make sure you are adding all necessary frameworks (that sometimes fixes strange leaks).
iWasRobbed
@IWasRobbed: all necessary frameworks ? it's not enough #import <MediaPlayer/MediaPlayer.h> ?
Cesar
@iWasRobbed doesn't the assignment to nil, as shown, leak the player, since it's a direct assignment to the object member? I think the idiom required is "self.player = nil", to invoke the setter, which *will* release the player before assigning the new value.
Seamus Campbell
@seamus: right you are! sorry I totally missed that... it's been a couple of long nights :)
iWasRobbed