Re: Why does this leak memory?
Re: Why does this leak memory?
- Subject: Re: Why does this leak memory?
- From: Bob Ippolito <email@hidden>
- Date: Mon, 11 Jul 2005 15:19:47 -1000
I wouldn't recommend reading that message, its implementation isn't
correct either. The rest of the thread probably corrects the
misinformation, but really, I dug out the Apple documentation that
clearly explains how to implement NSCopying in a subclass, read
that. Apple's documentation explains everything you need to know
about copyWithZone:, clearly and correctly, unlike the rest of this
thread and most threads previous (apparently).
-bob
On Jul 11, 2005, at 3:02 PM, Zach Wily wrote:
You create an instance of LayerCell, set the image of it, then just
throw it away. Calling [super copyWithZone:zone] makes a completely
new instance and returns it. Your LayerCell leaks, and the copy you
return doesn't have the image set.
Check out this link that was the first hit on google searching for
"copyWithZone:zone":
http://www.cocoabuilder.com/archive/message/cocoa/2004/11/5/120922
zach
On Jul 11, 2005, at 6:48 PM, Matt Ball wrote:
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
_______________________________________________
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
_______________________________________________
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