Re: release and reference counting query
Re: release and reference counting query
- Subject: Re: release and reference counting query
- From: Marc Stibane <email@hidden>
- Date: Wed, 19 Nov 2008 09:49:35 +0100
Am 18.11.2008 um 20:05 schrieb Andy Lee:
On Nov 18, 2008, at 1:33 PM, Marc Stibane wrote:
Am 07.11.2008 um 13:17 schrieb Roland King:
On Nov 7, 2008, at 7:59 PM, Calum Robertson wrote:
Below is a snippet of code from the "Creating an iPhone
Application" document from the iPhone DevCenter.
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
// Set up the view controller
UIViewController *aViewController = [[UIViewController alloc]
initWithNibName:@"MoveMeView" bundle:[NSBundle
mainBundle]];
self.viewController = aViewController;
[aViewController release];
...
}
[...]
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?
The above is not an init method and should not assume that it is
performing the first assignment to an ivar.
applicationDidFinishLaunching is definitely the first assignment, and
guaranted to be called once and only once.
In my code, I would even go so far as define the "viewController"
property readonly, since the root view controller never changes - so
there would be no setter method...
For all you know, a value may have been assigned to the ivar by the
init method, or by some code that executed between the init method
and this method.
If this were a normal (sub)view allocation, you were right. But this
is the root view allocation when the app starts.
The safer thing to do is use the accessor method to release any such
value if necessary.
Even if this were occurring in an init method, if the ivar happened
to be an inherited one, it would make sense to use an accessor to
set it in case the inherited init method assigned a value to the
ivar that needs to be released.
OK, so there might be cases where the bloated code is more safe. Fine.
I'll refrain from writing a radar bug against that code, but won't use
it myself though...
Thanks for your answer.
--
In a world without walls and fences,
who needs windows and gates?
_______________________________________________
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