views:

105

answers:

1
- (void)applicationDidFinishLaunching:(UIApplication *)application {

    // Create the navigation and view controllers
    RootViewController *rootViewController = [[RootViewController alloc]
                                              initWithStyle:UITableViewStylePlain];
    UINavigationController *aNavigationController = [[UINavigationController alloc]
                                             initWithRootViewController:rootViewController];
    self.navigationController = aNavigationController;
    [aNavigationController release];
    [rootViewController release];

    [rootViewController setRegions:[Region knownRegions]];

    // Configure and display the window
    [window addSubview:[navigationController view]];
    [window makeKeyAndVisible];
}

Here, in the above code the reference 'rootViewController' is used to send the message 'setRegions:', even after the object has been released in the previous line.

if it's wrong then how the simulator is running without any crash? or if it's right then, again how?, I couldn't see a difference between autorelease & release.

SOURCE:- http://developer.apple.com/iphone/library/samplecode/TableViewSuite/listing12.html

TO DOWNLOAD:- developer.apple.com/iphone/library/samplecode/TableViewSuite/index.html

+4  A: 

The object held by rootViewController was retained by aNavigationController so its retain count is 2, and aNavigationController was retained when it is assigned to self.navigationController so its retain count is 2. So when you release rootViewController and aNavigationController, their retain counts drop to 1 each, so they are not collected, so you can still access them through their references.

Edit

Objects are only collected once their retain counts hit 0, and any reference to the objects is still valid, (even if the reference was released) until such time. Granted normally you don't want to rely on this and should make the call prior to releasing the object, but in this case it does work.

Brandon Bodnár
Oh Yes. Thank You very much. A nice recall about reference counting.
selvam
I would say that although I normally make calls against objects before a final release, I can see the case in the above code where someone may want to keep the release calls as close to the retain calls as possible to make it clear what was happening.
Kendall Helmstetter Gelner