0

我在使用 pushViewController 时遇到问题 - 内存从 9 MB 变为 28 MB,但使用 popViewController 不会释放内存(应该变为 9 MB),但我得到了 28 MB。

下面是推送视图的代码。

/* 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

试图定位问题,但没有找到任何问题。

4

2 回答 2

1

你的问题在这里

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

您正在设置 mp = nil 而不释放它,当您在 dealloc 中释放时,它会将消息发送到 nil 而不是 mp 的实际实例。

释放 mp 然后将其设置为零。

于 2011-11-19T09:27:25.523 回答
0

如果视图控制器持有的任何引用仍在内存中,则不会调用 dealloc 方法。所以你需要在弹出视图控制器之前释放你的 mp 对象。有关更多信息,请参阅这篇文章。iPhone - 何时调用视图控制器的 dealloc?

于 2011-11-19T12:40:18.363 回答