Re: Trouble with property (copy) and retain counts
Re: Trouble with property (copy) and retain counts
- Subject: Re: Trouble with property (copy) and retain counts
- From: Malayil George <email@hidden>
- Date: Tue, 5 May 2009 18:54:21 -0400
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 <email@hidden> 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 (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
>>
>>
>
>
_______________________________________________
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