Quote:
Originally Posted by farhadf
I'm just starting iphone development and am getting my head around objective-C, the framework, etc.. Why does all sample code follow the following pattern:
Code:
UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
self.navigationController = aNavigationController;
[aNavigationController release];
where the object is first created, then assigned to the property, and then released, rather than:
Code:
self.navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
which seems simpler.
Thanks
|
The second version is a memory leak. You alloc/init the object, which gives you a retained copy, and then you assign it somewhere else without releasing it. You could do it in one line with an autorelease but in general it's better to do an explicit release as soon as you're done with something. If the object
could be released immediately but you've put autorelease on it, the object will stick around until the next time the autorelease pool drains. Probably not a big deal most of the time, but the iPhone is very tight on memory so why not squeeze every last little drop of space out of it?