2011-01-17 11 views
0

HELLO。 私のiPhoneアプリでMPMoviePlayerControllerを使用して、短いビデオクリップを表示することがあります。アプリのメインビューにはいくつかのボタンがあります。ボタンを押すと、ムービーが再生されます。 ボタンを押すと、ボタンが[self playVideo:@ "xxx"]を呼び出し、ビデオが正しく表示されます。 しかし、私がInstruments Allocations Toolでアプリを見ると、割り振られたメモリは最大8MB以上になり、プレイヤーが終了すると割り当てが解除されないことがわかります。ちょうど約15回ボタンを押すと、ipadが崩壊します。割り当ての責任は、CoreVideoと呼ばれる責任あるライブラリです。おそらく、ビデオのプリロードが完了した後にメモリがリークしても解放されないことがあります。どのようにこれらのメモリを解放することができます。MPMoviePlayerControllerプリロードメモリを解放する方法

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

ありがとう:ここ は、カテゴリー内のメソッドです!

答えて

1

コードにいくつかのメモリリークが含まれています。

リリースされる直前にnilに設定されているため、ここで割り当てられた文字列はリリースされませんでした。

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

コールplayVideoAtURL方法

-(void)playVideoAtURL:(NSURL *)theURL 
{ 
    // YOU HAVE MEMORY LEAK IN NEXT LINE!!! 

    theMovie = [[MPMoviePlayerViewController alloc] initWithContentURL:theURL]; 

    ... 
} 

そのためには、ケースの映画の中であなたがMPMoviePlayerViewControllerの新しいインスタンスを割り当て終わっていないと、以前のものを解放しないときは、MPMoviePlayerViewControllerを解放しません。

関連する問題