Skip navigation.
 
mlRe: Overriding -copyWithZone: the right way
FROM : Evan Schoenberg
DATE : Fri Nov 05 22:50:15 2004

On Nov 5, 2004, at 12:52 PM, Jean-Olivier Lanctôt wrote:

> Is there an Apple document that tells us how to do it exactly?
>
> I know we must return an object identical to "self" ... but do we
> invoke super copyWithZone? etc. etc.
>

If the superclass supports NSCopying, you should use [super 
copyWithZone:zone].  If it does not, you are implementing the 
base-level copyWithZone; a super call is impossible.  In that case, you 
use alloc and init as normal, using your setters to set all the current 
object's instance variables on the new one.

If the superclass supports copyWithZone and you therefore use [super 
copyWithZone:zone], keep in mind that your instance variables will be 
'lightly' (not sure the proper word) copied - they point to the same 
memory addresses but are NOT retained.  You therefore want to manually 
set all of your subclass's instasnce variables, like so:

//Copy
- (id)copyWithZone:(NSZone *)zone
{
   MyClass *newCell = [super copyWithZone:zone];

   /* Font is a retained NSFont* */
   [newCell setFont:font];
   
   /* subString is a retained NSString* */
   [newCell setSubString:subString];
   return(newCell);
}


>
> On Fri, 5 Nov 2004 19:31:36 +0100, M. Uli Kusterer
> <witness.of.<email_removed>> wrote:

>> At 17:46 Uhr +0100 05.11.2004, Michael Becker wrote:
>>
>>

>>> - (id)copyWithZone:(NSZone *)zone {
>>>      PCShoppingCartCell *copy = [[ PCShoppingCartCell alloc]
>>> initImageCell:nil];
>>>      return copy;
>>> }
>>>
>>> This does not crash, but it looks so suspiciously memory-leaking...
>>> (the alloc/init is not paired with a release on my side). When
>>> trying to follow the (few) suggestions the docs give me, I tried
>>> this (but it did not work, the app crashed as soon as the TableView
>>> wanted to redraw):

>>
>>  That's just fine. It doesn't leak, because methods with "copy" in
>> their name, by definition, return retained objects. So, whoever calls
>> this method knows they're responsible for releasing the object they
>> get.
>>
>>  I'm not sure though whether you shouldn't be calling [super
>> copyWithZone: zone] instead of allocating a new object, and then just
>> copy over those instance variables your subclass adds to the ones of
>> the superclass.
>> --
>> Cheers,
>> M. Uli Kusterer
>> ------------------------------------------------------------
>>        "The Witnesses of TeachText are everywhere..."
>>                    http://www.zathras.de
>>
>>
>>  _______________________________________________
>> Do not post admin requests to the list. They will be ignored.
>> Cocoa-dev mailing list      (<email_removed>)
>> Help/Unsubscribe/Update your Subscription:
>> http://lists.apple.com/mailman/options/cocoa-dev/
>> <email_removed>
>>
>> This email sent to <email_removed>
>>

>
>
> --
> --Olivier
>  _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Cocoa-dev mailing list      (<email_removed>)
> Help/Unsubscribe/Update your Subscription:
> http://lists.apple.com/mailman/options/cocoa-dev/evan.<email_removed>
>
> This email sent to evan.<email_removed>
>

Related mailsAuthorDate
mlOverriding -copyWithZone: the right way Michael Becker Nov 5, 17:46
mlRe: Overriding -copyWithZone: the right way Jonathon Mah Nov 5, 19:24
mlRe: Overriding -copyWithZone: the right way M. Uli Kusterer Nov 5, 19:31
mlRe: Overriding -copyWithZone: the right way Jean-Olivier Lanct… Nov 5, 19:52
mlRe: Overriding -copyWithZone: the right way Bill Garrison Nov 5, 22:42
mlRe: Overriding -copyWithZone: the right way Evan Schoenberg Nov 5, 22:50
mlRe: Overriding -copyWithZone: the right way M. Uli Kusterer Nov 5, 23:09
mlRe: Overriding -copyWithZone: the right way M. Uli Kusterer Nov 5, 23:24
mlRe: Overriding -copyWithZone: the right way Jean-Olivier Lanct… Nov 6, 00:27
mlRe: Overriding -copyWithZone: the right way Byron Wright Nov 6, 00:39
mlRe: Overriding -copyWithZone: the right way Jean-Olivier Lanct… Nov 6, 01:12
mlRe: Overriding -copyWithZone: the right way Steven Kramer Nov 6, 08:52
mlRe: Overriding -copyWithZone: the right way M. Uli Kusterer Nov 6, 16:50
mlRe: Overriding -copyWithZone: the right way The Karl Adam Nov 9, 02:04
mlRe: Overriding -copyWithZone: the right way The Karl Adam Nov 9, 02:24
mlRe: Overriding -copyWithZone: the right way M. Uli Kusterer Nov 9, 14:38