I was just reading through the boilerplate code for a universal app and I saw this:
- (void)dealloc
{
...
[_navigationController release];
...
}
and yet also:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
self.navigationController = [[[UINavigationController alloc] initWithRootViewController:masterViewController] autorelease];
...
}
I’m not sure I understand the whole underscore thing, but I gather that it’s to protect you from accessing the ivar directly, and instead accessing it through the property (why? I have no idea… but I accept that this is what its for).
However, what I don’t get is that if self.navigationController is autoreleased then surely this will cause issues when _navigationController is released at the end?
Even more confusing is the fact that there is no retain bit in the @property for navigationController.
So as far as I can tell _navigationController accessed through the property navigationController is autoreleased and then directly accessed and release in dealloc (where it could already be autoreleased surely?).
lol, I hope someone can explain what’s going on here! I’m sure the boilerplate code probably isn’t wrong!
Thank you.
Missing from the question are two important statements:
@propertyand@synthesize.My best guess is that these are:
and
Since the @property specifies retain when the setter is called, “self.navigationController =”, any existing value is released and the new value is retained. Thus an autorelease of the new value is counter acted by the retain in the setter.
The
@synthesizestatement says to use_navigationControlleras the ivar for the propertynavigationController.This is not ARC code.