• 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: j o a r <email@hidden>
  • Date: Mon, 5 May 2003 00:08:49 +0200

Sorry, have to reply to my own email as it didn't answer your question properly...

On Sunday, May 4, 2003, at 23:47 Europe/Stockholm, j o a r wrote:

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;
}

You notice that it sayd "or new" in the policy statement above, didn't you?
In any case, that was an unfortunate example. "new" is a shorthand for alloc+init that you should generally avoid to use.

Scrap my comment above...

The example you see is of an setter method. A setter method is used to assign an object to an instance variable. If you are handed an object and want to hold on to it for more than the current block of code you need to assign it to an instance (or global) variable and also retain it - to let the system know that you are interested in keeping the object around. This is typically done using setter methods.
The autorelease used before the new assignment is used to balance the retain that the current object referenced by the instance variable would have received when it was first assigned to the instance variable.

j o a r
_______________________________________________
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: Cocoa - Naive questions about memory (From: j o a r <email@hidden>)

  • Prev by Date: Re: Factory AutoRelease
  • Next by Date: Re: Cocoa - Naive questions about memory
  • Previous by thread: Re: Cocoa - Naive questions about memory
  • Next by thread: Re: Cocoa - Naive questions about memory
  • Index(es):
    • Date
    • Thread