2011-11-19 49 views
0

我在使用pushViewController時出現問題 - 內存從9 MB變爲28 MB,但使用popViewController不釋放內存(應該變爲9 MB),但我獲得了28 MB。未在iPhone應用程序中發佈的內存

下面是推動視圖的代碼。

/* Video Handler */ 
-(void)showVideo:(id)sender { 
    UIButton *btn    = (UIButton *)sender; 
    int nid       = btn.tag; 

    masterdb *mdbT   = [[masterdb alloc] init]; 
    izc_news *nclsT   = [mdbT getNewsDetail:nid]; 
    [mdbT release]; 
    NSString *vlink   = nclsT.VideoLink; 

    PlayVideo *vd    = [[PlayVideo alloc] init]; 
    vd.hidesBottomBarWhenPushed = YES; 
    vd.videoLink    = vlink; 

    [self.navigationController pushViewController:vd animated:YES]; 
    [vd release]; 
    vd         = nil; 
} 

下面是PlayVideo.h文件

#import <MediaPlayer/MediaPlayer.h> 

@interface PlayVideo : UIViewController { 
    NSString *videoLink; 
    MPMoviePlayerController *mp; 
    UIActivityIndicatorView *spinner; 
} 

@property(nonatomic, retain) NSString *videoLink; 
@property(nonatomic, retain) MPMoviePlayerController *mp; 
@property(nonatomic, retain) UIActivityIndicatorView *spinner; 

@end 

終於下面是試圖找到問題的PlayVideo.m文件

#import "PlayVideo.h" 


@implementation PlayVideo 

@synthesize videoLink; 
@synthesize mp; 
@synthesize spinner; 


- (void) viewDidLoad { 

    [super viewDidLoad]; 

    videoLink  = @"http://www.izooconnect.com/fwzNew/vids/testVid.mov"; 

    [[UIApplication sharedApplication] setStatusBarHidden:YES]; 

    [[self view] setBounds:CGRectMake(0, 0, 480, 320)]; 
    [[self view] setCenter:CGPointMake(160, 240)]; 
    [[self view] setTransform:CGAffineTransformMakeRotation(M_PI/2)]; 


    CGRect mainBounds    = [[UIScreen mainScreen] bounds]; 
    CGRect indicatorBounds = CGRectMake(mainBounds.size.height/2 - 24, mainBounds.size.width/2 - 24, 48, 48); 
    spinner         =   [[UIActivityIndicatorView alloc] initWithFrame:indicatorBounds]; 
    spinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite; 
    spinner.tag       = 1; 
    [spinner startAnimating]; 
    [self.view addSubview:spinner]; 


    mp = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString: videoLink]]; 


    [[NSNotificationCenter defaultCenter] addObserver:self 
                       selector:@selector(moviePreloadDidFinish:) 
                         name:MPMoviePlayerContentPreloadDidFinishNotification 
                        object:nil]; 


    [[NSNotificationCenter defaultCenter] addObserver:self 
                       selector:@selector(moviePlayBackDidFinish:) 
                         name:MPMoviePlayerPlaybackDidFinishNotification 
                        object:nil]; 


[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO]; 

    [[mp view] setFrame:CGRectMake(0, 0, 480, 320)]; 

    [mp setControlStyle:MPMovieControlStyleFullscreen]; 
    [mp setFullscreen:YES]; 

    [self.view addSubview:mp.view]; 

} 


- (void) moviePreloadDidFinish: (NSNotification *) notification { 
    UIActivityIndicatorView *tmpimg = (UIActivityIndicatorView *)[self.view viewWithTag:1]; 
    [tmpimg removeFromSuperview]; 
    [mp play]; 
} 


- (void) moviePlayBackDidFinish: (NSNotification *) notification { 
    [self.navigationController popViewControllerAnimated:YES]; 
} 




- (void) dealloc { 
    [mp release]; 
    [spinner release]; 
    [super dealloc]; 
} 

@end 

,但未能找到。

回答

1

你的問題是在這裏

- (void) moviePlayBackDidFinish: (NSNotification *) notification { 
    mp = nil; 
    [self.navigationController popViewControllerAnimated:YES]; 
} 

要設置MP =零不釋放它,當你的dealloc釋放,這將消息發送到零,而不是MP的實際情況。

釋放mp,然後將其設置爲零。

+1

mp = nil沒有在代碼中,增加釋放內存。 mp釋放也沒有幫助。 –