2012-04-16 65 views
0

我正在開發一個非常重視iPad的視頻沉重的iOS應用程序,它使用ARC,但似乎我有一個泄漏當我嘗試使用MPMoviePlayerController,儀器引發內存泄漏的代碼行,分配內存的視頻播放器對象,任何想法?當視頻完成播放時,清理視頻播放器似乎也不會發生。MPMoviePlayerController導致泄漏

任何幫助將是非常讚賞,到處找這個答案,你可以告訴這個問題是非常多的顯示塞與應用程序的性質。

代碼:

@interface ViewController() 
@property(nonatomic,strong) MPMoviePlayerController * vidPlayer; 
@end 

@implementation ViewController 
@synthesize vidPlayer; 

- (void)viewDidLoad 
    { 
     @autoreleasepool { 

     [super viewDidLoad]; 
     // Do any additional setup after loading the view, typically from a nib. 
     [self playVideoForFile:@"01_intro"]; 
     } 
    } 

    -(void)playVideoForFile:(NSString*)p_fileName 
    {  
      NSString *path = [[NSBundle mainBundle] pathForResource:p_fileName ofType:@"mp4"]; 
      NSURL *tempURI = [NSURL fileURLWithPath:path]; 
      vidPlayer = [[MPMoviePlayerController alloc] initWithContentURL:tempURI]; 

     [vidPlayer setControlStyle:MPMovieControlStyleNone]; 
     [vidPlayer setAllowsAirPlay:NO]; 

     [vidPlayer.view setFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.height,[[UIScreen mainScreen] bounds].size.width)]; 


     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(vidFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:vidPlayer]; 

     [vidPlayer play]; 

     [self.view addSubview:vidPlayer.view]; 

} 

-(void)vidFinishedCallback:(NSNotification*)aNotification 
{  
      [vidPlayer pause]; 
      vidPlayer.initialPlaybackTime = -1; 
      [vidPlayer stop]; 
      vidPlayer.initialPlaybackTime = -1; 
      [vidPlayer.view removeFromSuperview]; 
      vidPlayer = nil; 

     [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:vidPlayer]; 


} 
+0

你可以發佈儀器結果的屏幕截圖嗎? – Squatch 2012-04-16 17:30:45

回答

1

這是一種預感,但嘗試調用removeObserver 以前 vidPlayer被破壞。該文檔指定您應該「確保在notificationObserver或addObserver:selector:name:object:中指定的任何對象撤銷之前調用removeObserver:或removeObserver:name:object:」。 - NSNotification Center

此外,你也可以嘗試刪除在viewDidLoad中明確autoreleasepool。如果每個視圖只有一個vidPlayer,就不需要這麼做,並且在ARC和autoreleasepools中發現了一些近期發現的問題。見this bugfix

+0

感謝您對autoreleasepools的信息!並發現該男生錯誤,但沒有運氣仍然泄漏... http://www.welshkaratecollective.co.uk/screenshot_1.png http://www.welshkaratecollective.co.uk/screenshot_2.png – 2012-04-16 19:23:15

1

我試圖

MPMoviePlayerController *movieController = [notification object]; 
[[NSNotificationCenter defaultCenter] removeObserver:self 
    name:MPMoviePlayerPlaybackDidFinishNotification 
    object:movieController]; 
movieController = nil; 

,這似乎工作。

1

我有這個問題,它發送給我灰色!每次新視頻實例化時,它都不會被釋放(使用ARC),並且隨着更多的viewController實例被創建,它最終會導致崩潰。 我也檢查了所有MPMoviePlayer調用,確保它已停止並設置爲零。

問題不在於MPMoviePlayerController的分配和釋放,而在於用於傳遞視圖控制器細節的委託中。 在子視圖控制器,我有一個代表來檢查從tableViewController傳遞模型數據:

@property (strong, nonatomic) id<MyViewControllerDelegate> delegate; 

分配作爲一個強大的指針造成了巨大的內存泄漏。通過將其分配爲「弱」,它解決了問題。

@property (weak, nonatomic) id<MyViewControllerDelegate> delegate; 

作爲一般規則,對於視圖控制器委託,將它們分配爲弱屬性指針。

祝你好運。希望這可以消除泄漏!