简体   繁体   中英

Memory leak in MPMoviePlayerController

Can anyone tell me why I show a memory leak in instruments when I play a video? movieURL and moviePlayer are both retained, synthesized properties that are later released in dealloc. Thanks in advance for your help.

- (void)playMovie:(NSString *)movieString { 
NSLog(@"playMovie movieString: %@",movieString);
self.movieURL = [Utilities localMovieURLForFileName:movieString];
if (self.movieURL) {
    MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:self.movieURL];
    [[mp view] setFrame: [self.view bounds]];  // frame must match parent view
    [self.containerViewController.view addSubview: [mp view]];

    if (mp)
    {
            //save the movie player object
        self.moviePlayer = mp;
        [mp release];
        [self setUpMoviePlayer];

            // Apply the user specified settings to the movie player object
            //[self setMoviePlayerUserSettings];

            // Play the movie!
        [self.moviePlayer play];
    }
}
self.movieURL = nil;

}

The [mp release]; line does not need to be in the if statement. In Objective-C, you can send message to nil . So if your object was not allocated, that line won't crash as the init method will return nil .

Maybe that's why Instruments reports a memory leak, as it can't be assured your condition is met.

But your code seems to be valid. Also checks your properties, for copy or retain .

Instrument does tell you which line it was leaking memory so probably you could just pin down the line and tell us. In fact i think the whole if statement could be placed outside.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM