Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 06-30-2009, 07:53 PM   #2 (permalink)
jsd
at this moment
 
Join Date: Mar 2009
Location: San Francisco, CA
Posts: 900
jsd is on a distinguished road
Default

Quote:
Originally Posted by farhadf View Post
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?
jsd is offline   Reply With Quote
 

» Advertisements
» Stats
Members: 175,639
Threads: 94,102
Posts: 402,830
Top Poster: BrianSlick (7,990)
Welcome to our newest member, sCarlRodriguezj
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:09 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.