Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retaining rootViewController?

I wonder if someone can help me out with regards to the memory management in the code below. I am particularly interested in rootController, does it get retained when I do initWithRootViewController or does it instead (which is my guess) get retained with window addSubView: I am just curious what is happening ...

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    Base_TableViewController *rootController = [[Base_TableViewController alloc] init];
    navController = [[UINavigationController alloc] initWithRootViewController:rootController];
    [window addSubview:[navController view]];
    [window makeKeyAndVisible];

    [rootController release];
    return YES;
}

- (void)dealloc {
    [navController release];
    [window release];
    [super dealloc];
}

EDIT:

So essentially the code above is correct, the release at the bottom cancels out the alloc at the top, "rootController" is retained by navController?

Many thanks, much appreciated.

Gary

like image 837
fuzzygoat Avatar asked Jul 05 '26 20:07

fuzzygoat


1 Answers

After the call to alloc init, the retain count on rootController will be one. If navController does a retain in it's initWithRootViewController message, then after that line, it will have a retain count of two (I am pretty sure UINavigationController will retain it's root view controller).

Adding the navController's view to the window will not affect the rootController's retain count (It will increment the retain count on the UIView member of navController).

After the rootController release, it will decrement the retain count down to one.

Edit

Yep. In fact you could simplify the code a little more by removing the release at the bottom and sticking an autorelease around the initial allocation.

like image 73
RedBlueThing Avatar answered Jul 07 '26 10:07

RedBlueThing



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!