Re: new to memory mgmt
Re: new to memory mgmt
- Subject: Re: new to memory mgmt
- From: "Jonathan 'Wolf' Rentzsch" <email@hidden>
- Date: Sun, 15 Feb 2004 14:56:45 -0600
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!
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];
}
| Jonathan 'Wolf' Rentzsch
http://rentzsch.com
| Red Shed Software
http://redshed.net
| "better" necessarily means "different"
_______________________________________________
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.