Re: Beginner Question Re: Memory Management
Re: Beginner Question Re: Memory Management
- Subject: Re: Beginner Question Re: Memory Management
- From: "Henry McGilton (Boulevardier)" <email@hidden>
- Date: Mon, 22 Jun 2009 22:11:24 -0700
On Jun 22, 2009, at 1:39 PM, Daniel Torrey wrote:
I'm looking at some sample iPhone code, and in the app delegate's
applicationDidFinishLaunching method, I see
// Set up the view controller
MyViewController *aViewController = [[MyViewController alloc]
initWithNibName:@"HelloWorld" bundle:[NSBundle mainBundle]];
self.myViewController = aViewController;
[aViewController release];
I'm a little confused - I see an allocation, followed by an
assignment, followed by a release. I think that the assignment is
really a call to a setter - the myViewController field is created
automagically using the @property/@synthesize syntax.
Look at the @property declaration for myViewController in the .h
file. The likelihood is that there is a retain declared.
That means that when the setter method is invoked via the
self.myViewController = aViewController;
statement, a retain will be issued on the object during the
assignment. That statement is essentially
equivalent to writing the old-fashioned way:
[self setMyViewController: aViewController];
Since a release was sent to aViewController, what keeps that object
from being nuked at the end of the run loop? There must be another
retain happening somewhere, right?
Yes --- in the setter method, assuming the @property declaration
declared it as retain.
The code you cite above is a common idiom intended to funnel instance
variable accesses through their
proper accessor methods as opposed to accessing instance variables
directly.
Second question - is there anyway to see the code that gets
generated by @synthesize? I'm nosy and curious.
There's a few hints on how the accessors work (they work just fine,
thank you) starting around page 50
of The Objective C 2.0 Programming Language document. In general,
assume synthesised
accessor methods will Do The Right Thing . . .
Cheers,
. . . . . . . . Henry
_______________________________________________
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