0

我有一个非 ARC 项目。所以我正在维护它的内存管理。它有一个标签栏和导航控制器。在启动时,在显示标签栏之前,我必须显示一个 5 秒的启动视频。所以我有两个问题

在将标签栏控制器附加到主窗口之前显示视图控制器的最佳且简单的方法没有泄漏。以下是我当前的技术和代码,但代码分析器显示我的视频控制器中的潜在泄漏。

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

UIViewController *viewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil] ;
myNavigationController = [ [ UINavigationController alloc ] initWithRootViewController: viewController ];
[viewController release]; 

NSMutableArray *viewControllers;
viewControllers = [[NSMutableArray alloc] init];
[viewControllers addObject: myNavigationController];  //Tab 1
myNavigationController release];
// ADD Tab 2    //ADD Tab 3    //ADD Tab 4
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = viewControllers;
[viewControllers release];
//Add video contoller before showing tabs
self.videoController = [[VideoPlayViewController alloc] initWithNibName:@"VideoPlayViewController" bundle:nil];
[self.window addSubview:videoController.view];
[self.window makeKeyAndVisible];

这是我的视频播放器控制器代码

- (void)viewDidLoad
{
   [super viewDidLoad];
   MPMoviePlayerController *moviePlayer =  [[MPMoviePlayerController alloc] initWithContentURL:@"some url"];
    //------ init code in between and added observer to movie playback finish callback ------
   [self.view addSubview:moviePlayer.view ]; //show potential leak here if i not release moviePlayer
    //[moviePlayer release]; //if i release here controller show me black window with no video playing
}

- (void) moviePlayBackDidFinish:(NSNotification*)notification {

    MPMoviePlayerController *player = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerDidExitFullscreenNotification 
                                                  object:nil];
    [player stop];
    [player.view removeFromSuperview];
    [player release]; //show incorrect decrement of reference count of an object that is not owned at this point by caller
    //Fire notification to add tab bar as root view controller
}

播放视频后,我会在我的 appdelegate 中收到通知,然后

 [videoController.view removeFromSuperview];
 [self.videoController release];
 self.videoController = nil;
 self.window.rootViewController = self.tabBarController;

和我的主要应用程序委托 dealloc 像往常一样

- (void)dealloc {
    [_window release];
    [_tabBarController release];
    [super dealloc];
}

我想我正确地解释了我的问题。请任何人有更好的方法来做到这一点。

谢谢

4

1 回答 1

1

在外面声明moviePlayer变量viewDidLoad,然后在里面释放它moviePlayBackDidFinish。您正在添加对它的引用,然后仅删除该引用。您收到有关泄漏的通知的原因是它moviePlayer永远不会被释放 - 并且使用您的代码的当前设置,您无法发布它。

MPMoviePlayerController *moviePlayer; //keep reference to moviePlayer
- (void)viewDidLoad
{
    [super viewDidLoad];
    moviePlayer =  [[MPMoviePlayerController alloc] initWithContentURL:@"some url"];
    [self.view addSubview:moviePlayer.view ];
}

- (void) moviePlayBackDidFinish:(NSNotification*)notification {

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerDidExitFullscreenNotification 
                                                  object:nil];
    [moviePlayer stop];
    [moviePlayer.view removeFromSuperview];
    [moviePlayer release];
}
于 2012-07-27T12:50:21.923 回答