• 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: Zeroing out instance variables
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Zeroing out instance variables


  • Subject: Re: Zeroing out instance variables
  • From: Ken Thomases <email@hidden>
  • Date: Sat, 17 Apr 2010 10:21:26 -0500

On Apr 17, 2010, at 9:53 AM, Uli Kusterer wrote:

> On 17.04.10 16:47, Ken Thomases wrote:
>> On Apr 17, 2010, at 9:37 AM, Paul Sanders wrote:
>>>> That is, retain counts are stored outside of the instance.
>>> In that case there is a danger that a pointer to whatever is referenced would be transferred to the copy of the object, which is not what you want.
>>
>> No, you misunderstand.  There is (in the scheme I'm vaguely recalling) _nothing_ inside the instances which holds or points to something which holds the retain count.
>>
>> There is another data structure entirely which maps from object addresses to a retain count.
>
> The question is: Is it documented as a fact you can rely on that this is the case?

No, it's not, and I qualified it as unreliable.  We're on a bit of a tangent that was merely inspired by the original question.

> Tracking down subtle memory bugs caused by Apple deciding to move the reference count into an NSObject instance variable after all doesn't sound like a fun occupation.

Well, given what is documented, it seems unlikely that Apple could move it into an instance variable.  In particular, there are frequent mentions in the documentation that +[NSObject allocWithZone:] sets the 'isa' ivar and zeros out all other instance variables.  I suppose a retain count could be implemented in an instance variable such that the stored value is the logical value minus one, but it seems improbable.

<... a bit of Googling ...>

This <http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html> describes the isa pointer as "the NSObject class's sole public instance variable", although the qualifier "public" in there leaves wiggle room.  It also describes the retain count as "a kind of hidden instance variable managed by the runtime".

Ah-ha!  Maybe.  This <http://developer.apple.com/mac/library/documentation/cocoa/conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html> says "The NSObject class declares just one instance variable, isa, of type Class."  Although that may be just an example of the result of @encode(NSObject) rather than a definitive statement.

Anyway, a fun diversion, but, yes, I'd avoid writing production code based on these assumptions.

Cheers,
Ken

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Zeroing out instance variables (From: Ben Haller <email@hidden>)
 >Re: Zeroing out instance variables (From: Graham Cox <email@hidden>)
 >Re: Zeroing out instance variables (From: Ben Haller <email@hidden>)
 >Re: Zeroing out instance variables (From: "Paul Sanders" <email@hidden>)
 >Re: Zeroing out instance variables (From: Ben Haller <email@hidden>)
 >Re: Zeroing out instance variables (From: "Paul Sanders" <email@hidden>)
 >Re: Zeroing out instance variables (From: Ken Thomases <email@hidden>)
 >Re: Zeroing out instance variables (From: "Paul Sanders" <email@hidden>)
 >Re: Zeroing out instance variables (From: Ken Thomases <email@hidden>)
 >Re: Zeroing out instance variables (From: Uli Kusterer <email@hidden>)

  • Prev by Date: Re: Zeroing out instance variables
  • Next by Date: Re: Zeroing out instance variables
  • Previous by thread: Re: Zeroing out instance variables
  • Next by thread: Re: Zeroing out instance variables
  • Index(es):
    • Date
    • Thread