Skip navigation.
 
mlRe: Overriding -copyWithZone: the right way
FROM : Jonathon Mah
DATE : Fri Nov 05 19:24:25 2004

On 6 Nov 2004, at 03:16, Michael Becker wrote:

> I have a subclass of NSCell (for a TableView) that is supposed to
> display an image and an NSPopUpButtonCell. Everything works fine so
> far, but I obviously need to override -copyWithZone:. Here's what I
> came up with:
>
> - (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).


As far as I'm aware, returning a copy with retain count 1 is the right
thing to do. As you probably know, you need to pair alloc/release, as
well as copy/release (in other general code). Therefore a copied object
must be returned with a retain count of 1, or it'd be over-released.

> 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):
>
> - (id)copyWithZone:(NSZone *)zone
> {
>     PCShoppingCartCell *copy = [super copyWithZone:zone];
>     [ copy _initSubCells]; // This inits and sets up the NSPopUpButtonCell
>     return copy;
> }


What you're doing there is calling copyWithZone: on the superclass,
NSCell. This returns an NSCell (which wouldn't implement your own
_initSubCells method), not a PCShoppingCartCell.


Jonathon Mah
<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