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开发者_Go百科(@"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.
精彩评论