So yeah, I'm a Java guy in this crazy iPhone world. When it comes to memory management I stiill don't have a very good idea of what I'm doing.
I have an app that uses a navigation controller, and when it's time to go on to the next view I have code that looks like this:
UIViewController *myController = [[MyViewController alloc] initWithNibName:@"MyView"
bundle:[NSBundle mainBundle];
[[self navigationController] pushViewController:myController animated:YES];
Now according to Apple's fundamental rule on memory management
You take ownership of an object if you create it using a method whose name begins with “alloc” or “new” or contains “copy” (for example,
alloc
,newObject
, ormutableCopy
), or if you send it aretain
message. You are responsible for relinquishing ownership of objects you own usingrelease
orautorelease
. Any other time you receive an object, you must not release it.
To me that means that I should be releasing myController
, or giving it an autorelease
message. But, whenever I try doing that my application winds up crashing as I push and pop views off of the stack.
This didn't smell right to me, but in running Instruments it claims that I don't have any memory leaks.
So I my question is
- Am I doing this right?
- Is the Navigation Controller taking ownership of MyViewController, explaining the lack of a memory leak?
- Should I assign myController to an instance variable in my root ViewController? In that case it would be marked retain and I would release in the root's dealloc method