On 09/08/2012, at 9:39 AM, Greg Parker <gpar...@apple.com> 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 (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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

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

Reply via email to