Re: Memory management and Objects.(Newbie)
Re: Memory management and Objects.(Newbie)
- Subject: Re: Memory management and Objects.(Newbie)
- From: Nick Zitzmann <email@hidden>
- Date: Sat, 9 Oct 2004 14:17:29 -0600
On Oct 9, 2004, at 11:30 AM, Aaron Boothello wrote:
Thanks for the link. I read up on it as well as some o'reilly articles
on memory management. But im still unclear as to the implementation of
making 'deep' copies of instance variables in my subclasses.
i override copyWithZone like this:
- (id)copyWithZone:(NSZone *)zone
{
myTextureClass *copy = [super copyWithZone:zone];
[copy setImage:[self getImage]];
[copy setName:[self getName]];
return copy;
}
Are you sure that the object's superclass supports NSCopying? If not,
then what you're doing will not work. Instead, you ought to do
something like this: (warning - written in Mail, untested, use at your
own risk, etc.)
- (id)copyWithZone:(NSZone *)zone
{
id newCopy = [[[self class] allocWithZone:zone] init];
if (newCopy)
{
[newCopy setImage:[self getImage]];
[newCopy setName:[self getName]];
}
return newCopy;
}
Also, one other style note: -getImage should be titled -image; that way
it will work properly with KVC.
-(void)setImage:(NSImage *)img
{
image = [img copy];
}
Here your program may leak memory if "image" points to an object that
was already allocated. You should release it before copying, even if
it's a nil pointer.
Nick Zitzmann
<
http://www.chronosnet.com/>
_______________________________________________
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