views:

727

answers:

2

Hello,

I realise that a similar question has been posted before, but I really can't seem to find a solution that works for me. I have a MoviePlayer class which stores an ivar of MPMoviePlayerController, and I have the following method in the class:

-(void)playMovie:(NSString *)movieName
{
    NSURL *movieURL;
    NSBundle *bundle = [NSBundle mainBundle];
    if(bundle)
    {
        NSString *moviePath = [bundle pathForResource:movieName ofType:@"m4v"];
        if(moviePath)
        {
            movieURL = [NSURL fileURLWithPath:moviePath];
        }
    }
    MPMovieController *mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    if(mp)
    {
        self.moviePlayer = mp;
        [mp release];

        [self.moviePlayer play];
    }
    [movieURL release];
}

When call I play movie once the movie plays fine, but when it is called again on a different (or the same) movie file I get the following error stack:

_class_isInitialized
_class_lookupMethodAndLoadCache objc_msgSend
-[MoviePlayer setMoviePlayer:]
-[MoviePlayer playMovie:]

I'm not sure how to fix it! I assumed that when self.moviePlayer = mp is called then the current moviePlayer is released and the new one is added? The property is set to (nonatomic, retain). Can someone help please?

Thanks

+1  A: 

You have released the movie player. So it has been deallocated.

It seems you have released it elsewhere in your code, probably in the call back method. Just look for every instance you used it.

moviePlayer now points to garbage. So when you try to create a new moviePlayer, your property accessor attempts to send a release message to the garbage stored in moviePlayer.

if you want to deallocate moviePlayer between uses, don't deallocate it, instead set it to nil.

[self setMoviePlayer:nil];

Then when you try to create one you won't be messaging to garbage.

Corey Floyd
Hey, thanks for the reply. I have tried your suggestion and I get an error stack with the NSPopAutoreleasePool > [MPMoviePlayer Controller dealloc] method in. I never autoreleased the MPMoviePlayerController in my code, i've gone through every line just to double check it. Do MPMoviePlayerControllers get added to the pool automaticlly?
If you have the stack, then you should see where the movie player was released (hopefully).No, you used init, so it is your responsibility to release it (cocoa memory management).You'll have to track down when you are releasing the player.You can also make a category for MPMoviePlayer, then implement the release method. place a break point in that method and find out when it is being called.
Corey Floyd
A: 

Hey!

I fixed this problem. Turns out this code was the problem:

movieURL = [NSURL fileURLWithPath:moviePath];

The NSURL was being autoreleased too early for some reason. If I allocated the memory for this and released it myself, then the issue stopped happening.

Hopes this helps other people.

Stu