2011-01-17 4 views
0

HELLO입니다. 때때로 짧은 비디오 클립을 표시하기 위해 iPhone 응용 프로그램에서 MPMoviePlayerController를 사용하고 있습니다. 앱 mainView에 몇 가지 버튼이 있습니다. 버튼을 누르면 동영상이 재생됩니다. 버튼을 누르면 버튼이 [self playVideo : @ "xxx"]를 호출하고 비디오가 올바르게 표시됩니다. 그러나 계기 할당 도구로 앱을 볼 때 할당 된 메모리가 최대 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로 설정했기 때문에 여기에 할당 된 문자열은 출시되지 않았습니다. 전화 playVideoAtURL 방법

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

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

    ... 
} 

그 때문에이 경우 영화가 완료되지 않습니다 당신이 MPMoviePlayerViewController의 새로운 인스턴스를 할당하고 이전을 해제하지 않을 경우

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

당신은 MPMoviePlayerViewController를 해제하지 않습니다.

관련 문제