NSCell, copying, and objectValue
NSCell, copying, and objectValue
- Subject: NSCell, copying, and objectValue
- From: Matt Neuburg <email@hidden>
- Date: Thu, 02 Nov 2006 18:58:31 -0800
- Thread-topic: NSCell, copying, and objectValue
Is there something funny about how NSCell's setObjectValue works? Something
odd about how it copies and/or retains?
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