Re: Why does this leak memory?
Re: Why does this leak memory?
- Subject: Re: Why does this leak memory?
- From: Matt Ball <email@hidden>
- Date: Mon, 11 Jul 2005 20:48:09 -0400
Also, could someone tell me why this code is "bogus"?
- (id)copyWithZone:(NSZone *)zone
{
LayerCell *copy = [[[self class] alloc] init];
[copy setImage:[self image]];
return [super copyWithZone:zone];
}
- Matt Ball
On 7/11/05, Matt Ball <email@hidden> wrote:
> Okay, maybe I'll be able to figure this out if someone can answer one question:
>
> Why would copyWithZone: be called when I click on a row, but not when
> I add a row?
>
> - Matt Ball
>
> On 7/11/05, Bob Ippolito <email@hidden> wrote:
> > Well, keep reading the memory management documentation, particularly
> > the implementation of NSCopying section when the superclass
> > implements copyWithZone:, until you understand it.
> >
> > On Jul 11, 2005, at 2:09 PM, Matt Ball wrote:
> >
> > > I've reread the documentation several times, and every method I've
> > > tried has resulted in a crash or weird editing behavior except for
> > > this code...
> > >
> > > - Matt Ball
> > >
> > > On 7/11/05, Bob Ippolito <email@hidden> wrote:
> > >
> > >> Read the documentation. That's totally bogus code!
> > >>
> > >> On Jul 11, 2005, at 1:37 PM, Matt Ball wrote:
> > >>
> > >>
> > >>> Okay, I think we're getting closer. Right now, cell editing is
> > >>> working
> > >>> as expected. However, now I am leaking an instance of my custom
> > >>> cell.
> > >>> Here's my code:
> > >>>
> > >>> - (id)copyWithZone:(NSZone *)zone
> > >>> {
> > >>> LayerCell *copy = [[[self class] alloc] init];
> > >>> [copy setImage: [self image]];
> > >>> return [super copyWithZone:zone];
> > >>> }
> > >>>
> > >>> - Matt Ball
> > >>>
> > >>> On 7/11/05, Bob Ippolito <email@hidden> wrote:
> > >>>
> > >>>
> > >>>>
> > >>>> On Jul 11, 2005, at 1:08 PM, Bob Ippolito wrote:
> > >>>>
> > >>>>
> > >>>>
> > >>>>>
> > >>>>> On Jul 11, 2005, at 5:51 AM, glenn andreas wrote:
> > >>>>>
> > >>>>>
> > >>>>>
> > >>>>>
> > >>>>>>
> > >>>>>> On Jul 11, 2005, at 10:40 AM, Matt Ball wrote:
> > >>>>>>
> > >>>>>>
> > >>>>>>
> > >>>>>>
> > >>>>>>
> > >>>>>>> Interface:
> > >>>>>>>
> > >>>>>>> #import <Cocoa/Cocoa.h>
> > >>>>>>>
> > >>>>>>> @interface LayerCell : NSTextFieldCell
> > >>>>>>> {
> > >>>>>>> NSImage *image;
> > >>>>>>> }
> > >>>>>>>
> > >>>>>>> - (NSImage *)image;
> > >>>>>>>
> > >>>>>>> @end
> > >>>>>>>
> > >>>>>>> Implementation of NSCopying:
> > >>>>>>>
> > >>>>>>> - (id)copyWithZone:(NSZone *)zone
> > >>>>>>> {
> > >>>>>>> NSCell *copy = [[[self class] alloc] init];
> > >>>>>>>
> > >>>>>>> return copy;
> > >>>>>>> }
> > >>>>>>>
> > >>>>>>>
> > >>>>>>>
> > >>>>>>>
> > >>>>>>
> > >>>>>>
> > >>>>>> This does not implement copy correctly - the results are a new
> > >>>>>> object that doesn't have an image, so it's not a copy of the
> > >>>>>> original.
> > >>>>>>
> > >>>>>> You'll need to do something more like:
> > >>>>>>
> > >>>>>> - (id)copyWithZone:(NSZone *)zone
> > >>>>>> {
> > >>>>>> LayerCell *copy = [[[self class] alloc] init];
> > >>>>>> [copy setImage: [self image]];
> > >>>>>> return copy;
> > >>>>>> }
> > >>>>>>
> > >>>>>>
> > >>>>>>
> > >>>>>
> > >>>>> I'm pretty sure you want to be calling the super implementation of
> > >>>>> copyWithZone:, but then you have another problem (probably,
> > >>>>> anyway). See here:
> > >>>>>
> > >>>>> http://www.mulle-kybernetik.com/weblog/archives/000472.html
> > >>>>>
> > >>>>>
> > >>>>
> > >>>> And of course, reading the official documentation that tells you
> > >>>> how
> > >>>> to correctly implement NSCopying indispensable:
> > >>>>
> > >>>> http://developer.apple.com/documentation/Cocoa/Conceptual/
> > >>>> MemoryMgmt/
> > >>>> Tasks/ImplementCopy.html
> > >>>>
> > >>>> -bob
> > >>>>
> > >>>>
> > >>>>
> > >>>
> > >>>
> > >>
> > >>
> > >
> >
> >
>
_______________________________________________
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