• 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: RetainCount Question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: RetainCount Question


  • Subject: Re: RetainCount Question
  • From: Shawn Erickson <email@hidden>
  • Date: Thu, 26 May 2005 09:16:49 -0700


On May 26, 2005, at 8:40 AM, Mail5 wrote:

Lets say I have a simple method...

date = [[NSDate dateWithTimeIntervalSinceReferenceDate:0] retain]; <<< see below for question

Question:
The line ....
date = [[NSDate dateWithTimeIntervalSinceReferenceDate:0] retain];
date must be retained, yet, retaining it, sets the retain count to 2.


This does not make sense to me. Why should the retain count have to be 2?
As only one object references date.

On allocation objects have a retain count of one (implicit retain).

In this case allocation takes place in dateWithTimeIntervalSinceReferenceDate: and the caller (you) are expecting to get an object back. So dateWithTimeIntervalSinceReferenceDate: has to return an object to you and for the object to exist it must have a retain count greater then 0. Also as a result of the Cocoa memory contract it is expected that dateWithTimeIntervalSinceReferenceDate: must balance the implicit retain related to the allocation it did. To do that while still allowing the object to exist on return dateWithTimeIntervalSinceReferenceDate: sends the date object an autorelease message which puts that object in the current autorelease pool, this is the way to defer a retain until a later time, hence giving you the caller a chance to use the date object.

In your code example you send the date object a retain message since you want to hold onto that object for longer then the current autorelease pool. At the time you send retain the the date object already has a retain count of one because the current autorelease has not yet been dealloced/cleared. That is why you see a retain count of two but that is only temporary since at the end of the current autorelease pool the date object will be sent a release message to balance the implicit allocation related retain.

-Shawn


_______________________________________________ Do not post admin requests to the list. They will be ignored. Xcode-users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >RetainCount Question (From: Mail5 <email@hidden>)

  • Prev by Date: Re: RetainCount Question
  • Next by Date: Ambiguous warning disablement?
  • Previous by thread: Re: RetainCount Question
  • Next by thread: Ambiguous warning disablement?
  • Index(es):
    • Date
    • Thread