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 18:48:18 -0500
On Jul 10, 2005, at 6:34 PM, Matt Ball wrote:
I have a function which resizes an image and adds a drop shadow to it.
I've determined that it is leaking several NSImage instances. I've
taken out some code in order to narrow it down, and I've determined
that this is causing the leak:
- (NSImage *)shadowedImageWithImage:(NSImage *)image
{
[image setFlipped:YES];
// Resize the image, and make sure to antialias it.
float thumbnailHeight = [tableView rowHeight] * 0.85;
float thumbnailWidth = (thumbnailHeight / [image size].height) *
[image size].width;
NSImage *resizedImage = [[NSImage alloc]
initWithSize:NSMakeSize(thumbnailHeight, thumbnailWidth)];
[resizedImage lockFocus];
[NSGraphicsContext saveGraphicsState];
[[NSGraphicsContext currentContext]
setImageInterpolation:NSImageInterpolationHigh];
[image drawInRect:NSMakeRect(0.0, 0.0, thumbnailWidth,
thumbnailHeight) fromRect:NSMakeRect(0.0, 0.0, [image size].width,
[image size].height) operation:NSCompositeCopy fraction:1.0];
[NSGraphicsContext restoreGraphicsState];
[resizedImage unlockFocus];
image = [resizedImage retain];
[resizedImage release];
Ok, the last two lines have the exact same effect as:
image = resizedImage;
IOW, all you are doing is make the pointer image point to the object
resizedImage was point to, at the same point increasing its retain
count. Then in the next line you decrease its retain count. Then:
return image;
}
You return it. Your entire last three lines could be summarized as:
return resizedImage;
Problem is, your alloc +initWithSize: is still in effect. So
resizedImage has too much retain count. Try using:
return [resizedImage autorelease];
instead of the last three lines.
Could anyone help me figure out why this is leaking? I've read up on
retain counts, and I think that when I retain "resizedImage," I
increase the retain count such that [resizedImage release]; doesn't
completely release it.
Any help would be greatly appreciated.
-- Matt Ball
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