Re: NSCell, copying, and objectValue
Re: NSCell, copying, and objectValue
- Subject: Re: NSCell, copying, and objectValue
- From: "Ken Ferry" <email@hidden>
- Date: Fri, 3 Nov 2006 05:08:37 -0800
On 11/2/06, Matt Neuburg <email@hidden> wrote:
Is there something funny about how NSCell's setObjectValue works?
Something
odd about how it copies and/or retains?
-[NSCell setObjectValue:] copies. These things should be better documented,
but for the general rule of thumb on copying vs. retaining see "Accessor
Methods" in the Cocoa Fundamentals Guide, <http://tinyurl.com/y979wm>. The
object value is an attribute of the cell (as opposed to a relationship like
representedObject), so it is copied.
In this case you could also take a cue from the declaration:
- (void)setObjectValue:(id <NSCopying>)object
The object has to conform to NSCopying because it is copied.
So setObjectValue copies the object and raises the retain count on the
copy?
Is this weird or is it just me? m.
The test is misleading.. -[NSCell objectValue] retains and autoreleases the
value before returning it (<http://tinyurl.com/y979wm> again), so the 3
retains you report are understandable.
Like everyone says, paying attention to retainCount is usually not useful.
Try Object Alloc. It can keep a backtrace for every retain, release and
autorelease message sent in your app. Look for something that isn't
balanced.
-Ken
I am trying a strategy where I return a complicated object in
outlineView:objectValueForTableColumn:..., so I can use this object (via
-[NSCell objectValue] in my custom NSCell's draw code.
The problem is that I'm in one of those situation where I'm leaking these
objects, and as I tweak my code trying to prevent this I get crashes. So
it's leak or crash (or both maybe). I hate that. :)
So I got thinking about how NSCells get copied a lot, and I tried this:
- (id)copyWithZone:(NSZone *)zone {
NSLog(@"cell copy starting: objectValue is %p, its retain count is
%i",
[self objectValue], [[self objectValue] retainCount]);
NSTwoImageAndTextCell *cell = [super copyWithZone: zone];
NSLog(@"cell copy made: objectValue is %p, its retain count is %i",
[cell objectValue], [[cell objectValue] retainCount]);
return cell;
}
And the log says things like:
-> cell copy starting: objectValue is 0x3ceb40, its retain count is 3
-> cell copy made: objectValue is 0x340600, its retain count is 3
So the objectValues of the original cell and its copy are two different
objects, and yet the objectValue of the copy (which just got created) has
a
retainCount of 3?
I also tried overriding setObjectValue: to see how it behaves:
- (void)setObjectValue:(id <NSCopying>)object {
NSLog(@"setting object value: incoming object is %p, its retain count
is
%i", object, [object retainCount]);
[super setObjectValue:object];
NSLog(@"did set object value: my object is %p, its retain count is
%i",
[self objectValue], [[self objectValue] retainCount]);
}
And the log says things like:
-> setting object value: incoming object is 0x3e11c40, its retain count is
2
-> did set object value: my object is 0x3e125e0, its retain count is 3
So setObjectValue copies the object and raises the retain count on the
copy?
Is this weird or is it just me? m.
--
matt neuburg, phd = email@hidden, http://www.tidbits.com/matt/
pantes anthropoi tou eidenai oregontai phusei
AppleScript: the Definitive Guide - Second Edition!
http://www.amazon.com/gp/product/0596102119
Take Control of Word 2004, Tiger, and more -
http://www.takecontrolbooks.com/tiger-customizing.html
Subscribe to TidBITS! It's free and smart. http://www.tidbits.com/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden