• 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: "Paul Sanders" <email@hidden>
  • Date: Sat, 17 Apr 2010 16:11:33 +0100

> 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.

Yes I see, that's also what Jean-Daniel is saying.  In which case, my bytewise copy scheme should work, since if a and b are (or derive from) NSObject then if I do this:

    *b = *a;

the retain count of (b) will be unaffected (which is what you want) and remains independent of (a) since retain counts are tied to object addresses, not content.  The code I posted before should therefore produce the following results (prediction, untested, fingers crossed):

    NSObject *a = [[NSObject alloc] init];
    printf ("%d\n", [a retainCount]);    // 1
    NSObject *b = [[[NSObject alloc] init] retain];
    printf ("%d\n", [b retainCount]);    // 2
    *b = *a;
    printf ("%d\n", [b retainCount]);    // 2
    [b retain];
    printf ("%d\n", [b retainCount]);    // 3
    printf ("%d\n", [a retainCount]);    // 1

I reckon my scheme might actually be faster Ben, if you benchmark it.  Without the assert it's just a method call and a memcpy, which the compiler knows the size of and can therefore optimise.  Only you can decide whether either approach is worth the risk.

Paul 'quick and dirty' Sanders.
_______________________________________________

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

  • Follow-Ups:
    • Re: Zeroing out instance variables
      • From: Ben Haller <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>)

  • 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