Re: NSCopyObject is a disaster (was Re: Another NSOutlineView issue)
Re: NSCopyObject is a disaster (was Re: Another NSOutlineView issue)
- Subject: Re: NSCopyObject is a disaster (was Re: Another NSOutlineView issue)
- From: Graham Cox <email@hidden>
- Date: Thu, 09 Aug 2012 09:52:35 +1000
On 09/08/2012, at 9:39 AM, Greg Parker <email@hidden> wrote:
> NSCopyObject() is ugly. Avoid it if you can.
>
> NSCell uses NSCopyObject. I don't know if there are other framework classes that are likely to be copied and subclassed that use NSCopyObject.
>
> One solution is to compile your subclass with ARC. ARC-compiled ivars get correct retain count management inside NSCopyObject. (You still need to override -copyWithZone: if you want those ivars to be copied instead of retained, though.)
I see that NSCopyObject is deprecated as of 10.8 (but is still being used internally).
This is going to be fun moving forward :) I'm not sure how binary compatibility is going to be maintained as NSCopyObject disappears, for example, in a cell subclass I might have:
- (id) copyWithZone:(NSZone*) zone
{
MyCell* copy = [super copyWithZone:zone];
[copy->someInternalPointer retain];
return copy;
}
This relies on the existing behaviour of NSCopyObject not retaining pointers it has copied.
If in future NSCell changes to use some alternative such as the ARC stuff you mention, this will now leak due to the extra retain. If I don't do the retain, it will crash on older systems due to the (later) over-release.
Is there a solution to this that will work forwards and backwards?
Incidentally I did find buried in an insignificant corner of the documentation that NSCell only copies pointers, in "Subclassing NSCell": "If the subclass contains instance variables that hold pointers to objects, consider overriding copyWithZone: to duplicate the objects. The default version copies only pointers to the objects". IMHO this should be in huge red letters in the NSCell class reference, with its consequences properly spelt out.
--Graham
_______________________________________________
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