Re: Trouble with property (copy) and retain counts

2009-05-06 Thread mmalc Crawford


On May 5, 2009, at 4:04 PM, Malayil George wrote:


I don't think I have a leak...quite the opposite. I think the sample  
app I'm
playing with is crashing because I might be releasing an object too  
soon.

Which is why I resorted to retainCounts to try and track it down...




mmalc


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Trouble with property (copy) and retain counts

2009-05-06 Thread Jean-Daniel Dupas

Search NSZombie in Google. It may be what you need.

Le 6 mai 09 à 01:04, Malayil George a écrit :

I don't think I have a leak...quite the opposite. I think the sample  
app I'm
playing with is crashing because I might be releasing an object too  
soon.

Which is why I resorted to retainCounts to try and track it down...

George


On Tue, May 5, 2009 at 7:01 PM, Nick Zitzmann   
wrote:




On May 5, 2009, at 4:54 PM, Malayil George wrote:

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?




Yes - don't worry about retain counts unless Instruments or the  
leaks tool
is telling you that you have a memory leak. Do you know for a fact  
that you

have a memory leak?

Nick Zitzmann






___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/devlists%40shadowlab.org

This email sent to devli...@shadowlab.org



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Trouble with property (copy) and retain counts

2009-05-05 Thread Stephen J. Butler
Beyond the fact that retain counts are useless for debugging...

On Tue, May 5, 2009 at 5:54 PM, Malayil George  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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Trouble with property (copy) and retain counts

2009-05-05 Thread Nick Zitzmann


On May 5, 2009, at 5:04 PM, Malayil George wrote:

I don't think I have a leak...quite the opposite. I think the sample  
app I'm
playing with is crashing because I might be releasing an object too  
soon.

Which is why I resorted to retainCounts to try and track it down...



You can track that down in Instruments, too. Use the object alloc  
instrument and turn on retain/release logging before you start. Then  
you'll get full stack traces when an object is created/retained/ 
released/deallocated.


Nick Zitzmann




___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Trouble with property (copy) and retain counts

2009-05-05 Thread Malayil George
I don't think I have a leak...quite the opposite. I think the sample app I'm
playing with is crashing because I might be releasing an object too soon.
Which is why I resorted to retainCounts to try and track it down...

George


On Tue, May 5, 2009 at 7:01 PM, Nick Zitzmann  wrote:

>
> On May 5, 2009, at 4:54 PM, Malayil George wrote:
>
>  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?
>>
>
>
> Yes - don't worry about retain counts unless Instruments or the leaks tool
> is telling you that you have a memory leak. Do you know for a fact that you
> have a memory leak?
>
> Nick Zitzmann
> 
>
>
>
>
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Trouble with property (copy) and retain counts

2009-05-05 Thread Nick Zitzmann


On May 5, 2009, at 4:54 PM, Malayil George wrote:


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?



Yes - don't worry about retain counts unless Instruments or the leaks  
tool is telling you that you have a memory leak. Do you know for a  
fact that you have a memory leak?


Nick Zitzmann




___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Trouble with property (copy) and retain counts

2009-05-05 Thread Malayil George
Hi,   Thanks for the replies. Things are a bit clearer now, that I
understand the synthesized getters are returning retained and auto-released
objects. However, the behavior doesn't seem to be entirely consistent. I
have marked in comments below what I would expect the retain count to be
NSImage *img = [[NSImage alloc] init]; //retain count should be
1

Cell *cell = [[Cell alloc] init];

[cell setImage:img]; //retain count should be 1

NSImage *cellImage = [cell image]; //retain count 2

NSLog(@"Original image pointer: %x, retain count: %d", img, [img retainCount]);
//retain count 1

NSLog(@"cellImage pointer:%x, retain count: %d", cellImage, [cellImage
retainCount]); //retain count 2

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

NSLog(@"Draining pool");

[pool drain];

pool = nil;

pool = [[NSAutoreleasePool alloc] init];

NSLog(@"Cell image pointer: %x, retain count: %d", [cell image], [[cell
image] retainCount]); //retain count 3

NSLog(@"Cell image pointer: %x, retain count: %d", [cell image], [[cell
image] retainCount]); //retain count 5


Actual program output:

  2009-05-05 18:50:03.404 TestApp[2688:10b] Original image pointer: 112bd0,
retain count: 1

*  2009-05-05 18:50:03.422 TestApp[2688:10b] cellImage pointer:113410,
retain count: 2*

*  2009-05-05 18:50:03.427 TestApp[2688:10b] Cell image pointer: 113410,
retain count: 3*

*  2009-05-05 18:50:03.428 TestApp[2688:10b] Cell image pointer: 113410,
retain count: 5*

*  2009-05-05 18:50:03.428 TestApp[2688:10b] Draining pool*

*  2009-05-05 18:50:03.428 TestApp[2688:10b] cellImage pointer:113410,
retain count: 1*

*  2009-05-05 18:50:03.429 TestApp[2688:10b] Cell image pointer: 113410,
retain count: 2*

*  2009-05-05 18:50:03.429 TestApp[2688:10b] Cell image pointer: 113410,
retain count: 4*

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

Regards

George M.P.



On Tue, May 5, 2009 at 11:40 AM, Ali Ozer  wrote:

> There is no trouble. The getter (the -image call in your case) returns a
> retained/autoreleased result, which temporarily pushes up the reference
> count on each call.  If you were to ask for the retainCount later in the
> program, you should see that it has gone back to normal. Something like the
> following will demonstrate this:
>
> NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
> NSLog(@"Cell image pointer: %x, retain count: %d", [cell image], [[cell
> image] retainCount]);
> NSLog(@"Cell image pointer: %x, retain count: %d", [cell image], [[cell
> image] retainCount]);
> [pool drain];
> NSLog(@"Cell image pointer: %x, retain count: %d", [cell image], [[cell
> image] retainCount]);
>
> The -drain will eliminate those extra "references" (which are harmless of
> course).   No need to do this in your code, unless you need to use a local
> autorelease pool for another reason.
> Ali
>
>
>
> On May 4, 2009, at 21:34 PM, Malayil George wrote:
>
>
>  Hi,I'm a little confused with retain counts and properties. I would
>> appreciate some help in understanding this. I have a class setup as
>> follows
>>
>> @interface Cell : NSObject {
>>
>> NSImage *image;
>>
>> }
>>
>>
>> @property (copy) NSImage *image;
>>
>>
>> @end
>>
>> Now, in my code, I do the following
>>
>> NSImage *img = [[NSImage alloc] init];
>>
>> Cell *cell = [[Cell alloc] init];
>>
>> [cell setImage:img];
>>
>> NSLog(@"Original image pointer: %x, retain count: %d", img,[img
>> retainCount
>> ]);
>>
>> NSLog(@"Cell image pointer: %x, retain count: %d", cell.image, [cell.image
>> retainCount]);
>>
>> NSLog(@"Cell image pointer: %x, retain count: %d", [cell image], [[cell
>> image] retainCount]);
>>
>> It looks like everytime I call [cell image] the retain count goes up. Why
>> would this be the case? It doesn't matter if I set the property to copy or
>> retain, I see the same results. Am I expected to do the following
>>
>> NSImage *img = [cell image];
>>
>> NSLog(@"Cell image pointer: %x, retain count: %d", [image retainCount]);
>>
>> I guess my question is do getters automatically increment the retain count
>> instead of my having to do [[cell image] retain]? Thanks
>>
>> Regards
>>
>> George M.P.
>> ___
>>
>> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
>>
>> 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:
>> http://lists.apple.com/mailman/option

Re: Trouble with property (copy) and retain counts

2009-05-05 Thread Jean-Daniel Dupas
If the retain count confuses you, you stop to using it as it's almost  
never relevant.



That said, have a look at the atomic section in the property references:

http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocProperties.html


Le 5 mai 09 à 06:34, Malayil George a écrit :

Hi,I'm a little confused with retain counts and properties. I  
would
appreciate some help in understanding this. I have a class setup as  
follows


@interface Cell : NSObject {

NSImage *image;

}


@property (copy) NSImage *image;


@end

Now, in my code, I do the following

NSImage *img = [[NSImage alloc] init];

Cell *cell = [[Cell alloc] init];

[cell setImage:img];

NSLog(@"Original image pointer: %x, retain count: %d", img,[img  
retainCount

]);

NSLog(@"Cell image pointer: %x, retain count: %d", cell.image,  
[cell.image

retainCount]);

NSLog(@"Cell image pointer: %x, retain count: %d", [cell image],  
[[cell

image] retainCount]);

It looks like everytime I call [cell image] the retain count goes  
up. Why
would this be the case? It doesn't matter if I set the property to  
copy or

retain, I see the same results. Am I expected to do the following

NSImage *img = [cell image];

NSLog(@"Cell image pointer: %x, retain count: %d", [image  
retainCount]);


I guess my question is do getters automatically increment the retain  
count

instead of my having to do [[cell image] retain]? Thanks

Regards

George M.P.
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/devlists%40shadowlab.org

This email sent to devli...@shadowlab.org



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com