• 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: Matt Ball <email@hidden>
  • Date: Mon, 11 Jul 2005 18:35:51 -0400

By /**/, you mean around copyWithZone:, right? I uncommented it, and
the crashes stop. However, now when I double click the cell to edit,
the background turns white, the text gets selected, but I can't edit
it or change the selection.

- Matt Ball

On 7/11/05, Ryan Stevens <email@hidden> wrote:
> Now that I think about it, I remember having a similar problem and it turned
> out that it was selectWithFrame:.. and/or editWithFrame:.. related. Try
> getting rid of those (/**/) and see if the crashes persist. If not, there ya
> go.
>
> You can see what I did here: CCDTextFieldCell
> (http://www.cocoadev.com/index.pl?CCDTextFieldCell)
>
>
> On Jul 11, 2005, at 2:48 PM, Matt Ball wrote:
>
> It's no longer leaking; it just crashes when I click on a row - I can
> add as many rows as I want, however, as long as I don't click on any.
> When I add in my copyWithZone: method, the crashes stop, but my cell
> editing gets weird. Here's my copyWithZone: code:
>
> - (id)copyWithZone:(NSZone *)zone
> {
>     LayerCell *copy = [[[self class] alloc] init];
>     [copy setImage: [self image]];
>     return copy;
> }
>
> - Matt Ball
>
> On 7/11/05, Ryan Stevens <email@hidden> wrote:
>
> Bummer. Is it still crashing, leaking or both?
>
> And another thing to try..
>
> - (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell
> forTableColumn:(NSTableColumn *)tableColumn row:(int)row
> {
>      NSMutableArray *layersArray = [[[NSDocumentController
> sharedDocumentController] currentDocument] layersArray];
>      NSMutableDictionary *layerDictionary = [layersArray
> objectAtIndex:row];
>      if(layersArray != nil) {
>          if([[tableColumn identifier]
> isEqualToString:@"titleColumn"]) {
>              NSImage *layerImage = [[layerDictionary
> objectForKey:@"Image"] copy];
>              NSImage *shadowedImage = [NSImage
> shadowedImageWithImage:layerImage
> rowHeight:[layersView rowHeight]];
>
>                  NSLog(@"layerImage: %@", layerImage);
>                  NSLog(@"shadowedImage: %@", shadowedImage);
>
>                  [layerImage release];
>
>              if(shadowedImage != nil)
>                  [cell setImage:shadowedImage];
>          }
>      }
> }
>
>
>
>
> On Jul 11, 2005, at 2:18 PM, Matt Ball wrote:
>
>
>
> No change. :/
>
> - Matt Ball
>
> On 7/11/05, Ryan Stevens <email@hidden> wrote:
>
>
>
> It's just a guess but maybe it's getting released out from under
> you so try
> retaining and releasing it in shadowedImageWithImage:..
>
> + (NSImage *)shadowedImageWithImage:(NSImage *)image
> rowHeight:(int)rowHeight
> {
>     if (!image) return nil; // if it doesn't exist you should bail
> here.
>
>     [image retain]; // make sure it sticks around
>
>     float thumbnailHeight = 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];
>     //    [image setFlipped:NO];
>
>     int squareSize = 3;
>     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(2*squareSize, 2*squareSize)];
>     [transparencyPattern lockFocus];
>     [[NSColor colorWithCalibratedWhite:0.8 alpha:1.0] set];
>     NSRectFill(NSMakeRect(0,0,squareSize,squareSize));
>
> NSRectFill(NSMakeRect(squareSize,squareSize,squareSize,squareSize));
>     [[NSColor whiteColor] set];
>
> NSRectFill(NSMakeRect(squareSize,0,squareSize,squareSize));
>
> NSRectFill(NSMakeRect(0,squareSize,squareSize,squareSize));
>     [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];
>     [image release]; // set it free
>
>     return [shadowCanvas autorelease];
> }
>
>
>
>
> On Jul 11, 2005, at 2:04 PM, Matt Ball wrote:
>
> "image" comes from a dictionary within an array. Each row has a
> dictionary assigned to it which stores a variety of information about
> it. The image is one of those things. Also, I rewrote the original
> shadowedImageWithImage code a long ways back in the chain of
> messages.
> It is currently:
>
> + (NSImage *)shadowedImageWithImage:(NSImage *)image
> rowHeight:(int)rowHeight
> {
>     float thumbnailHeight = 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];
>     //    [image setFlipped:NO];
>
>
>     int squareSize = 3;
>     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(2*squareSize, 2*squareSize)];
>     [transparencyPattern lockFocus];
>     [[NSColor colorWithCalibratedWhite:0.8 alpha:1.0] set];
>     NSRectFill(NSMakeRect(0,0,squareSize,squareSize));
>
> NSRectFill(NSMakeRect(squareSize,squareSize,squareSize,squareSize));
>     [[NSColor whiteColor] set];
>
> NSRectFill(NSMakeRect(squareSize,0,squareSize,squareSize));
>
> NSRectFill(NSMakeRect(0,squareSize,squareSize,squareSize));
>     [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];
> }
>
> - Matt Ball
>
> On 7/11/05, Ryan Stevens <email@hidden> wrote:
>
> Okay...
>
> I would try retaining 'image' at the beginning of
> shadowedImageWithImage: and releasing it before returning (since it's
> not clear to me where this image really comes from or who owns it).
> This way it's guaranteed to stick around long enough for you to draw
> it into 'resizedImage'...
>
> + (NSImage *)shadowedImageWithImage:(NSImage *)image
> {
>      if (!image) return nil;
>
>      [image retain]; // we want this to stick around a bit
>      [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 release]; // we don't need this anymore.
>
>      return [resizedImage autorelease];
> }
>
>
>
 _______________________________________________
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

References: 
 >Why does this leak memory? (From: Matt Ball <email@hidden>)
 >Re: Why does this leak memory? (From: Matt Ball <email@hidden>)
 >Re: Why does this leak memory? (From: Matt Ball <email@hidden>)

  • Prev by Date: Re: gui without Interface Builder
  • Next by Date: Re: State based on front most document (was RE: Documentation frustrations)
  • Previous by thread: Re: Why does this leak memory?
  • Next by thread: RE: Why does this leak memory?
  • Index(es):
    • Date
    • Thread