0

HELLO. I'm using a MPMoviePlayerController in my iPhone app to display some short video clips sometimes. There are some buttons in the app mainView . When I press the button, the movie will be play. When I press the button,the button call [self playVideo: @"xxx"],and the video shows correctly.But when I see the app with Instruments Allocations Tool I see that the memory allocated gets up to 8+ MB and is not deallocated after the player finished. if I press the button just about 15th times, the ipad will collapse. The responsible for the allocations is a responsible Library called CoreVideo . Maybe the memory leaks when the video preload but don't release after it's finished. How i can release these memory. Here are the methods in the Category:

-(id)playVideo:(NSString* )videoName
{
    NSString* s = [[NSBundle mainBundle] pathForResource:videoName ofType:@"mp4"];
    NSURL* url = [NSURL fileURLWithPath:s];
    [self playVideoAtURL: url];
    s = nil;
    [s release];
    url = nil;
    [url release];
}


-(void)playVideoAtURL:(NSURL *)theURL
{
    theMovie = [[MPMoviePlayerViewController alloc] initWithContentURL:theURL];
    theMovie.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
    theMovie.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
    if (LeftOrRight == 0) {
        [theMovie.view setTransform: CGAffineTransformMakeRotation(degreesToRadians(-90))];
    }
    else if (LeftOrRight == 1) {
        [theMovie.view setTransform: CGAffineTransformMakeRotation(degreesToRadians(90))];
    }
    CGRect screenBounds = [[UIScreen mainScreen] bounds];  
    theMovie.view.frame = screenBounds;
    theMovie.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
    [theMovie.moviePlayer prepareToPlay]; 
    [self presentModalViewController: theMovie animated: YES]; 

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(myMovieFinishedCallback:)
                                                 name: MPMoviePlayerPlaybackDidFinishNotification
                                               object:nil]; 
}

-(void)myMovieFinishedCallback:(NSNotification *)aNotification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:nil];
    [theMovie  dismissMoviePlayerViewControllerAnimated];
    [theMovie.moviePlayer stop];
    [theMovie release];
}

Thanks!

e40pud
  • 336
  • 2
  • 18
zhao
  • 1
  • 2

1 Answers1

1

Your code has several memory leaks.

String allocated here has never been released, because you set it to nil just before releasing.

NSString* s = [[NSBundle mainBundle] pathForResource:videoName ofType:@"mp4"];
s = nil;
[s release];

You do not release MPMoviePlayerViewController in playVideoAtURL method

- (void)playVideoAtURL:(NSURL *)theURL
{
    // YOU HAVE MEMORY LEAK IN NEXT LINE!!!
    
    theMovie = [[MPMoviePlayerViewController alloc] initWithContentURL:theURL];
    
    ...
}

Therefor, there is a chance that you will allocate a new instance of MPMoviePlayerViewController without releasing an existing one in myMovieFinishedCallback.

e40pud
  • 336
  • 2
  • 18