Re: Why does this leak memory?
Re: Why does this leak memory?
- Subject: Re: Why does this leak memory?
- From: Charilaos Skiadas <email@hidden>
- Date: Sun, 10 Jul 2005 19:25:11 -0500
On Jul 10, 2005, at 7:12 PM, Matt Ball wrote:
- (void)setImage:(NSImage *)anImage
{
if(anImage != image && anImage != nil)
{
image = [anImage retain];
[anImage release];
}
}
Here is your problem. This makes image point to anImage, but nothing
is retaining anImage, so the moment you exit the method, image
becomes invalidated, and boom.
Try:
- (void)setImage:(NSImage *)anImage
{
if(anImage != image && anImage != nil)
{
[image release];
image = [anImage retain];
}
}
Also, this line:
image = [shadowCanvas retain];
will now leak the object that image was pointing to before this call.
Also, there is a number of redundant lines, and calls to copy, retain
and release, that I will leave to you to figure out. It will benefit
you a lot more.
The key thing to understand, is that retain does not return a new
pointer or something. It just increases the retain count of the
object, and returns the object. I.e. whatever you set equal to
[shadowCanvas retain] is pointing to exactly the same thing as
shadowCanvas does, no difference at all between them. They are
completely equivalent from that point on.
Haris
_______________________________________________
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