Re: new to memory mgmt
Re: new to memory mgmt
- Subject: Re: new to memory mgmt
- From: pmcurry <email@hidden>
- Date: Sun, 15 Feb 2004 14:15:07 -0800
On Feb 15, 2004, at 12:56 PM, Jonathan 'Wolf' Rentzsch wrote:
pmcurry, email@hidden, wrote:
switch ( message ) {
<snip>
case: kBegin
Node n = [[Node alloc] init];
[n setRank:a];
[n setParent:b];
[n setState:c];
[tree addObject:n]
[n release];
break;
<snip>
} // switch
I think you mean:
case kBegin:
But's that minor -- someone should build a syntax checker into
Mail.app!
I'll buy that for a dollar!
An alternative pattern is:
Node *n = [[[Node alloc] init] autorelease];
[n setRank:a];
[n setParent:b];
[n setState:c];
[tree addObject:n]
break;
This will prevent you from leaking "n" if an exception is raised after
you allocate the object but before you release it.
Since alloc+init+autorelease is so common, often the class will have a
convenience builder method like so:
Node *n = [Node node]; // often also nodeWithParam:something
[n setRank:a];
[n setParent:b];
[n setState:c];
[tree addObject:n]
break;
Where +[node] is:
@implementation Node
+ (id)node {
return [[[Node alloc] init] autorelease];
}
Good suggestion. I think I like that. Particularly after the light bulb
fired up after reading Louis Sacha's response.
Thanks.
-Phil
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.