views:

659

answers:

1

I have a custom viewController called SourceListViewController, and I'm adding it into a UINavigationController, the view of which is then added to the window of the iphone App. After passing the SourceListViewController to UINavigationController, I release the sourceListViewController.

SourceListViewController *sourceListVC = [[SourceListViewController alloc] initWithNibName:@"SourceListViewController" bundle:nil];
UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:sourceListVC] autorelease];
[sourceListVC release];

When I do this, the app would crash after the view is loaded onto the phone. When I commented out the last line, the app work fine. Isn't initWithRootViewController supposed to retain the copy of sourceListVC?

+6  A: 

You are autoreleasing navigationController. So if navigationController gets autoreleased (which will probably happen in the next runloop) then so will sourceListVC.

Diederik Hoogenboom