Re: Why does this leak memory?
Re: Why does this leak memory?
- Subject: Re: Why does this leak memory?
- From: Matt Ball <email@hidden>
- Date: Sun, 10 Jul 2005 21:41:40 -0400
Okay, I've done some retooling, but I'm still getting some leaks/crashes...
Here's my code:
- (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell
forTableColumn:(NSTableColumn *)tableColumn row:(int)row
{
int indexToQueryForImage = row * 6 + 1;
NSImage *image = [array objectAtIndex:indexToQueryForImage];
image = [self shadowedImageWithImage:image];
if(image != nil)
[cell setImage:image];
}
- (NSImage *)shadowedImageWithImage:(NSImage *)image
{
float thumbnailHeight = [layersView rowHeight] * 0.85;
float thumbnailWidth = (thumbnailHeight / [image size].height) *
[image size].width;
NSSize thumbnailSize = NSMakeSize(thumbnailHeight, thumbnailWidth);
[image setFlipped:YES];
NSImage *resizedImage = [[NSImage alloc] initWithSize:thumbnailSize];
[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];
NSImage *imageCanvas = [[NSImage alloc] initWithSize:thumbnailSize];
[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);
[resizedImage compositeToPoint:NSMakePoint(0, 0)
operation:NSCompositeSourceOver];
[imageCanvas unlockFocus];
[transparencyPattern release];
thumbnailSize.width += 4;
thumbnailSize.height += 4;
NSImage *shadowCanvas = [[NSImage alloc] initWithSize:thumbnailSize];
[shadowCanvas lockFocus];
NSShadow *theShadow = [[NSShadow alloc] init];
[theShadow setShadowColor:[NSColor blackColor]];
[theShadow setShadowBlurRadius:4];
[theShadow set];
[imageCanvas compositeToPoint:NSMakePoint(2, 1)
operation:NSCompositeSourceOver];
[shadowCanvas unlockFocus];
[theShadow release];
[resizedImage release];
[imageCanvas release];
return [shadowCanvas autorelease];
}
And in the custom NSCell subclass:
- (void)dealloc {
[image release];
[super dealloc];
}
- (void)setImage:(NSImage *)anImage
{
if(anImage != image && anImage != nil)
{
// [image release]; <-- I get a crash if I uncomment this line
image = [anImage retain];
}
}
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
// Draw the thumbnail image
if (image != nil) {
NSSize imageSize;
NSRect imageFrame;
imageSize = [image size];
NSDivideRect(cellFrame, &imageFrame, &cellFrame, 10 +
imageSize.width, NSMinXEdge);
if ([self drawsBackground]) {
[[self backgroundColor] set];
NSRectFill(imageFrame);
}
imageFrame.origin.x += 3;
imageFrame.size = imageSize;
imageFrame.origin.y = NSMinY(cellFrame) + (cellFrame.size.height -
[image size].height)/2;
[image drawInRect:imageFrame fromRect:NSMakeRect(0, 0, [image
size].width, [image size].height) operation:NSCompositeSourceOver
fraction:1.0];
}
// Modify the cell frame to compensate for the image
cellFrame.origin.y = NSMinY(cellFrame) + cellFrame.size.height/4 + 2;
if([self isHighlighted])
[self setTextColor:[NSColor alternateSelectedControlTextColor]];
else
[self setTextColor:[NSColor textColor]];
// Draw the cell's text normally
[super drawWithFrame:cellFrame inView:controlView];
}
_______________________________________________
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