views:

18

answers:

1

At the end of this code:

UIViewController *viewController = [[UIViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
[viewController release];
[self presentModalViewController:navigationController animated:YES];
[navigationController release];

The navigation controller appear modally, as usual, but when I click a button on the viewController, it crashes. In fact, the viewController has a retain count of 0. If I comment one of the two releases everything went better than expected. I have been seeing this code pretty much everywhere, what could be wrong?

+3  A: 

The code you've posted is correct, but somewhere else you're over releasing something.

A few things to note:

First, never trust retainCount.

Second, make sure you're properly managing the memory of your nib objects as outlined here.

Finally, you'll need to use the NSZombie detection in Instruments to find out where your real problem lies. This video provides a nice how to.

Robot K
For the retainCount, I'm pretty sure I'm using it correctly. I'm not using any nib except for the default MainWindow.xib. Not sure if NSZombie will help in this case, but actually I should give it a try.
gurghet
The point is that you *can't* use retainCount correctly. The number that's returned is not to be trusted. It's a meaningless number.
Robot K
I know but if the app is idle, after a second or two, it usually gives a useful number. I never used NSZombie with Instruments, looks much more practical!
gurghet
From the trace, it seems that the "zombie" isn't the viewController, but actually the "self" viewController, and apparently I'm not responsible for any release message.
gurghet
You have a zombie because you've either called `release` somewhere you shouldn't have, or your missing a `retain` that you need. I suggest you review Apple's Memory Management Programming Guide. http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html
Robot K
I apparently fixed the thing calling presentModalViewController from [self parentVC]. Anyways, from the trace it seems like the VC and the NC own each other (?) because they dealloc together (how cute)
gurghet