• 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: Cocoa - Naive questions about memory
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Cocoa - Naive questions about memory


  • Subject: Re: Cocoa - Naive questions about memory
  • From: Jeffrey Mattox <email@hidden>
  • Date: Mon, 5 May 2003 01:02:59 -0500

At 2:22 PM -0700 5/4/03, Danny Swarzman wrote:
Cocoa therefore sets this policy: If you create an object (using alloc,
allocWithZone:, or new) or copy an object
(using copy, copyWithZone:, mutableCopy,or mutableCopyWithZone:), you alone
are responsible for releasing it.
If you did not directly create or copy the object, you do not own it and
should not release it.

Then there is sample code that appears to do the opposite:

- (void)setMainSprocket:(Sprocket *)newSprocket
{
[mainSprocket autorelease];
mainSprocket = [newSprocket retain]; /* Claim the new Sprocket. */
return;
}

Why is it necessary to call autorelease for he object to which mainSprocket
pointed before setMainSprocket is

Some people know too much. :-) The replies so far obfuscate the answer.

Simply put, the autorelease is done because it was you who retained the object the previous time setMainSprocket: was called. So the autorelease above balances the retain from the *previous* time.

(Okay, so the first time, mainSprocket will be nil, but [nil autorelease] is allowed, as I learned here a few weeks ago. And the truly anal programmer will release the last mainSprocket when quitting, thus the whole world stays balanced.)

Jeff
_______________________________________________
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: 
 >Cocoa - Naive questions about memory (From: Danny Swarzman <email@hidden>)

  • Prev by Date: Re: getting the type of an object of a Dictionary
  • Next by Date: NSDictionary displayed in NSBrowser or NSOutlineView
  • Previous by thread: Re: Cocoa - Naive questions about memory
  • Next by thread: Re: Cocoa - Naive questions about memory
  • Index(es):
    • Date
    • Thread