• 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: "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.

  • Follow-Ups:
    • Re: new to memory mgmt
      • From: pmcurry <email@hidden>
  • Prev by Date: Half-view scolling in NSTableView
  • Next by Date: Re: new to memory mgmt
  • Previous by thread: Re: new to memory mgmt
  • Next by thread: Re: new to memory mgmt
  • Index(es):
    • Date
    • Thread