• 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: Retain, then autorelease and then release does what?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Retain, then autorelease and then release does what?


  • Subject: Re: Retain, then autorelease and then release does what?
  • From: Nathan Day <email@hidden>
  • Date: Sat, 4 May 2002 20:25:11 +0930

On Saturday, May 4, 2002, at 04:09 PM, Joel Rosenblum wrote:

I read on Apple's site that "If you need to use an object after disposing of its owner or rendering it invalid by some other means, you can retain and autorelease it before sending the message that would invalidate it."

This seems odd. It would make sense to retain it, then autorelease it at the end of the method, but if you retain and autorelease it at the same time, isn't that having zero net effect, thus allowing the subsequent release to release the object as it would without this code?

A zero net effect is what you want in the long run otherwise you would have a memory leak. The purpose of autoreleasing is to delay the release, so it occurs later on. Retaining increases the retain count now, autoreleasing gives the object to an autorelease pool so it can decrease its retain count some time later on.


Also, I'd like a bit of clarification on one point: if I have a property of a class which the class retains, when the class is released, will all of its objects automatically be released as well, or must I explicitly release each of them at that time?

Yes and yes.

You have to over ride the dealloc method and release any objects you may have retained so that when your objects retain count reaches zero it's dealloc method is called and it any objects you have retained are also released.
_______________________________________________
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: 
 >Retain, then autorelease and then release does what? (From: Joel Rosenblum <email@hidden>)

  • Prev by Date: linkPath: toPath:
  • Next by Date: Re: Accessing window in document applications
  • Previous by thread: Re: Retain, then autorelease and then release does what?
  • Next by thread: Re: Retain, then autorelease and then release does what?
  • Index(es):
    • Date
    • Thread