• 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: Trouble with property (copy) and retain counts
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Trouble with property (copy) and retain counts


  • Subject: Re: Trouble with property (copy) and retain counts
  • From: "Stephen J. Butler" <email@hidden>
  • Date: Tue, 5 May 2009 18:15:36 -0500

Beyond the fact that retain counts are useless for debugging...

On Tue, May 5, 2009 at 5:54 PM, Malayil George <email@hidden> wrote:
> NSLog(@"Cell image pointer: %x, retain count: %d", [cell image], [[cell
> image] retainCount]); //retain count 4
>
> NSLog(@"Cell image pointer: %x, retain count: %d", [cell image], [[cell
> image] retainCount]); //retain count 6
>
> [snip]
>
> If the getter is always incrementing the reference count by 1, I would
> expect the reference count at each NSLog, where I am using the getter, to be
> 2 above the previous value. This doesn't seem to be the case for line 3 of
> the output where it has gone up by only 1. In subsequent NSLog() statements
> it seems to go up by 2.
>
> This behavior is seen again after the drain. Am I missing something? Thanks

Yes. What you're missing is that the order function arguments are
evaluated in is undefined, That is, you expect this:

NSImage *temp1 = [cell image];
NSImage *temp2 = [cell image];
NSLog(@"Cell image pointer: %x, retain count: %d", temp1, [temp2 retainCount]);

But because of the way GCC pushes arguments on the stack, what you may get is:

NSImage *temp2 = [cell image];
NSImage *temp1 = [cell image];
NSLog(@"Cell image pointer: %x, retain count: %d", temp1, [temp2 retainCount]);

Or more explicitly, you could get:

NSImage *temp2 = [cell image];
NSImage *temp1 = [cell image];
NSUInteger temp3 = [temp2 image];
NSLog(@"Cell image pointer: %x, retain count: %d", temp1, temp3);

Or maybe:

NSImage *temp2 = [cell image];
NSUInteger temp3 = [temp2 image];
NSImage *temp1 = [cell image];
NSLog(@"Cell image pointer: %x, retain count: %d", temp1, temp3);

Or maybe:

NSImage *temp1 = [cell image];
NSImage *temp2 = [cell image];
NSUInteger temp3 = [temp2 image];
NSLog(@"Cell image pointer: %x, retain count: %d", temp1, temp3);

The general point is, you can NEVER rely on the order function
arguments are evaluated. This is particularly unpredictable when, in
this case, the evaluations have side effects on each other. Any order
is possible.
_______________________________________________

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: 
 >Trouble with property (copy) and retain counts (From: Malayil George <email@hidden>)
 >Re: Trouble with property (copy) and retain counts (From: Malayil George <email@hidden>)

  • Prev by Date: Re: Trouble with property (copy) and retain counts
  • Next by Date: Re: How remove a clip path?
  • Previous by thread: Re: Trouble with property (copy) and retain counts
  • Next by thread: WebView loses script context
  • Index(es):
    • Date
    • Thread