Re: Why does this leak memory?
Re: Why does this leak memory?
- Subject: Re: Why does this leak memory?
- From: Zach Wily <email@hidden>
- Date: Sun, 10 Jul 2005 18:28:04 -0600
Please see my inline comments. I find at least 3 leaks in this method.
- (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)];
So the object pointed to by resizedImage now has a retain count of 1.
[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];
The object pointed to by resizedImage now has a retain count of 2.
[resizedImage release];
The object pointed to by resizedImage is back to a retain count of 1.
image is now also pointing to that object.
// Place our new image on a background
NSImage *imageCanvas = [[NSImage alloc] init];
[imageCanvas setSize:[image size]];
[imageCanvas lockFocus];
NSRect imageFrame = NSMakeRect(0, 0, [imageCanvas size].width,
[imageCanvas size].height);
NSImage *transparencyPattern = [[NSImage alloc]
initWithSize:NSMakeSize(4, 4)];
[transparencyPattern lockFocus];
[[NSColor colorWithCalibratedWhite:0.8 alpha:1.0] set];
NSRectFill(NSMakeRect(0,0,2,2));
NSRectFill(NSMakeRect(2,2,2,2));
[[NSColor whiteColor] set];
NSRectFill(NSMakeRect(2,0,2,2));
NSRectFill(NSMakeRect(0,2,2,2));
[transparencyPattern unlockFocus];
[[NSColor colorWithPatternImage:transparencyPattern] set];
NSRectFill(imageFrame);
[image compositeToPoint:NSMakePoint(0, 0)
operation:NSCompositeSourceOver];
[imageCanvas unlockFocus];
image = [imageCanvas copy];
Whoops! Just lost our pointer to the original object pointed to by
resizedImage, and it has a retain count of 1, so it leaks.
image is now pointing to an NSImage with a retain count of 1.
[transparencyPattern release];
[imageCanvas release];
NSImage *shadowCanvas = [[NSImage alloc]
initWithSize:NSMakeSize([image size].width+4, [image size].height+4)];
[shadowCanvas lockFocus];
NSShadow *theShadow = [[NSShadow alloc] init];
[theShadow setShadowColor:[NSColor blackColor]];
[theShadow setShadowBlurRadius:4];
[theShadow set];
[image compositeToPoint:NSMakePoint(2, 1)
operation:NSCompositeSourceOver];
[shadowCanvas unlockFocus];
image = [shadowCanvas retain];
Whoops! We just lost another object with a retain count of 1, so it
leaks.
image is not pointing to an NSImage with a retain count of 2.
[theShadow release];
[shadowCanvas release];
The retain count of that object pointed to by image went down to 1.
return image;
Here you're returning an NSImage with a retain count of 1. You should
autorelease it, or it will leak (assuming the calling method behaves
like it should and assumes it's autoreleased.)
_______________________________________________
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