2011-03-06 55 views
0

任何人都可以告訴我爲什麼我在播放視頻時在樂器中顯示內存泄漏? movieURL和moviePlayer都保留,這些屬性稍後會在dealloc中釋放。在此先感謝您的幫助。MPMoviePlayerController中的內存泄漏

- (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; 

}

回答

0

[mp release];線並不需要是在if聲明。 在Objective-C中,您可以發送消息至nil。所以如果你的對象沒有被分配,這條線不會崩潰,因爲init方法將返回nil

也許這就是儀器報告內存泄漏的原因,因爲它無法保證您的條件得到滿足。

但是你的代碼似乎是有效的。 還檢查您的屬性,爲copyretain

+0

謝謝。屬性是保留的。這是正確的,對嗎? – intomo 2011-03-06 01:07:38

+0

只要你在你的dealloc方法中釋放它們,是的 – Macmade 2011-03-06 10:08:52

0

儀器確實告訴你它正在泄漏內存的哪一行,因此您可能只需將該行關閉並告訴我們。事實上,我認爲整個如果陳述可以放在外面。