• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: new to memory mgmt
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

References: 
 >Re: new to memory mgmt (From: "Jonathan 'Wolf' Rentzsch" <email@hidden>)

  • Prev by Date: Re: new to memory mgmt
  • Next by Date: Re: [Slightly OT] Shareware donation collection
  • Previous by thread: Re: new to memory mgmt
  • Next by thread: Re: new to memory mgmt
  • Index(es):
    • Date
    • Thread