• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Why does this leak memory?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why does this leak memory?


  • Subject: Re: Why does this leak memory?
  • From: Ryan Stevens <email@hidden>
  • Date: Mon, 11 Jul 2005 13:06:00 -0700

How did our vets miss this? You didn't fix it, you applied a fragile band-aid where there was no wound.

That method has no business changing what 'image' points to, it should use 'image' to create and return a shadowed version. That's all.

Stick my version of your method in a category and see if your new problem doesn't go away.

On Jul 11, 2005, at 11:51 AM, Matt Ball wrote:

Thanks for the response. I've resolved the memory leak, but now for
some reason, my cell acts really weird when I try to edit it. Here is
a description of my problem:
http://www.cocoabuilder.com/archive/message/cocoa/2005/7/11/141550

- Matt Ball

On 7/11/05, Ryan Stevens <email@hidden> wrote:


On Jul 10, 2005, at 4: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];

    return image;
}

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.

I would put this in a category and write it like so ...

+ (NSImage *)shadowedImageWithImage:(NSImage *)image
{
    if (!image) return nil;

    [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];

    return [resizedImage autorelease];
}


It seems to me that image = [resizedImage retain]; is your problem. You
don't want to do that there. Instead...


- (void)doTheSameThingMinusTheClobber
{
NSImage *shadowed = [NSImage shadowedImageWithImage:image];

    if (!shadowed) return; // NSLog(@"ahh!");

    [image release];
    image = [shadowed retain];
}


_______________________________________________ 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
  • Follow-Ups:
    • Re: Why does this leak memory?
      • From: Matt Ball <email@hidden>
References: 
 >Why does this leak memory? (From: Matt Ball <email@hidden>)
 >Re: Why does this leak memory? (From: Ryan Stevens <email@hidden>)
 >Re: Why does this leak memory? (From: Matt Ball <email@hidden>)

  • Prev by Date: Strange NSCell Editing (was Re: Why does this leak memory?)
  • Next by Date: Re: Why does this leak memory?
  • Previous by thread: Strange NSCell Editing (was Re: Why does this leak memory?)
  • Next by thread: Re: Why does this leak memory?
  • Index(es):
    • Date
    • Thread