I have an non ARC project. So i am maintaining its memory management. It has an tabbar and navigation controller.In that on launch before showing tab bar i have to show an launch video of 5 sec kind of thing. so i have two question
Best and easy way to show an view controller before attaching tab bar controller to main window without leaks.Following is my current technique and code but code analyzer is showing me potential leaks in my video controller.
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];
Here is my Video Player controller code
- (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
}
And after video is played i get notificaton in my appdelegate and then
[videoController.view removeFromSuperview];
[self.videoController release];
self.videoController = nil;
self.window.rootViewController = self.tabBarController;
and my main app delegate dealloc as usual
- (void)dealloc {
[_window release];
[_tabBarController release];
[super dealloc];
}
i think i explained my problem correctly.Please anyone have any better way to do this.
Thanks
Declare the
moviePlayervariable outside ofviewDidLoadand then release it inmoviePlayBackDidFinish. You’re adding a reference to it and then removing only that reference. The reason you’re being notified about a leak is thatmoviePlayeris never released – and with the current setup of your code you can’t release it.