Re: release and reference counting query
Re: release and reference counting query
- Subject: Re: release and reference counting query
- From: Peter N Lewis <email@hidden>
- Date: Wed, 19 Nov 2008 10:24:42 +0900
At 19:33 +0100 18/11/08, Marc Stibane wrote:
what's the reason for defining a local variable aViewController to
receive the UIViewController pointer, then copying that to the
instance variable with a setter method which increases the retain
count, then decrease the retain count again - instead of just using
the instance variable?
viewController = [[UIViewController alloc]
initWithNibName:@"MoveMeView" bundle:[NSBundle mainBundle]];
since "viewController" is a member of the class, you don't need the "self.".
viewController = xxx simply assigns the ivar.
self.viewController = xxx is actually syntactic sugar for
[self setViewController:xxx]
which means bindings and observing stuff might be happening, as well
as releasing old versions.
I would actually write:
self.viewController = [[[UIViewController alloc]
initWithNibName:@"MoveMeView" bundle:[NSBundle mainBundle]] autorelease];
The up side of this is:
* less code than the original
* setter is classed
* memory retention is left to the setter
* any old value of viewController is released as necessary
* autorelease is a good safe paradigm
The downside compared to the original:
* slightly less efficient because of the autorelease call
The downside compared to the ivar assignment version:
* Somewhat less efficient because of the autorelease and the setter method call
However, in this case it is a view allocation, which will be far more
expensive that the autorelease or the setter, and presumably very
infrequently executed, so the advantages in safe, clean, clear,
simple, short code easily outweigh the efficiency issues.
IMO of course. The original method will work equally well and si
more efficient - the only downside is the extra lines of code. The
ivar setting method is much more risky - changes far away from this
code could introduce bugs.
Enjoy,
Peter.
--
Keyboard Maestro 3 Now Available!
Now With Status Menu triggers!
Keyboard Maestro <http://www.keyboardmaestro.com/> Macros for your Mac
<http://www.stairways.com/> <http://download.stairways.com/>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden