• 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
Shouldn't dataCellForRow return an autoreleased object?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Shouldn't dataCellForRow return an autoreleased object?


  • Subject: Shouldn't dataCellForRow return an autoreleased object?
  • From: Jerry Krinock <email@hidden>
  • Date: Thu, 02 Mar 2006 08:54:07 -0800
  • Thread-topic: Shouldn't dataCellForRow return an autoreleased object?

I have not been able to find many examples of over-riding dataCellForRow: to
return a subclass of NSCell, but the few that I've found look like this:

- (id)dataCellForRow:(int)iRow  {
    MySubclassedCell * thisCell = [[MySubclassedCell alloc] init];

    [cell setThis] ;
    [cell setThat] ;
    // etc.

    return cell ;
}

Isn't that a leak?  According to my understanding of the Cocoa memory
management contract, since I allocced cell, I should autorelease it before
returning, and the caller should retain it.

But when I add an autorelease message there, my app crashes sporadically
while editing text in the table.  What is wrong with my understanding?

Jerry

(Specifically, MySubclassedCell has an NSImage as an instance variable.  The
sometimes-crash occurs when an instance of MySubclassedCell is deallocced,
specifically when this NSImage is released.)

Here is MySubclassedCell..............

@interface MySubclassedCell : NSTextFieldCell
{
    NSImage* _icon1 ;
    NSImage* _icon2 ; // for future use
}
@end


@implementation MySubclassedCell

- (void)setIcon1:(NSImage*)newIcon {
    [newIcon retain] ;
    // Crashes on next line if dataCellForRow returns autoreleased:
    [_icon1 release] ;
    _icon1 = newIcon ;
}

- (NSImage*)icon1 {
    return _icon1 ;
}

- (void)setIcon2:(NSImage*)newIcon {
    [newIcon retain] ;
    [_icon2 release] ;
    _icon2 = newIcon ;
}

- (NSImage*)icon2 {
    return _icon2 ;
}

- (id)init {
    if ((self = [super init])) {
        [self setIcon1:nil] ;
        [self setIcon2:nil] ;
    }

    return self ;
}

- (void)dealloc {
    [self setIcon1:nil] ;
    [self setIcon2:nil] ;
    [super dealloc];
}

// This is an over-ride of an NSCell method
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
    NSImage* icon1 = [self icon1] ;

    if (icon1 != nil) {
        NSSize    icon1Size;
        NSRect    icon1Frame;

        [icon1 setScalesWhenResized:YES] ;
        icon1Size.height = cellFrame.size.height ;
        icon1Size.width = cellFrame.size.height ;
        [icon1 setSize:icon1Size];
        NSDivideRect(cellFrame, &icon1Frame, &cellFrame, icon1Size.width,
NSMinXEdge);

        if ([self drawsBackground]) {
            [[self backgroundColor] set];
            NSRectFill(icon1Frame);
        }

        // Since Quartz starts at lower left but images start at upper
left?......
        if ([controlView isFlipped])
            icon1Frame.origin.y += ceil((cellFrame.size.height +
icon1Frame.size.height) / 2);
        else
            icon1Frame.origin.y += ceil((cellFrame.size.height -
icon1Frame.size.height) / 2);

        //[icon1 drawAtPoint:icon1Frame.origin fromRect:icon1Frame
operation:NSCompositeSourceOver fraction:1.0];

        [icon1 compositeToPoint:icon1Frame.origin
operation:NSCompositePlusDarker];
    }

    [self setAttributedStringValue:[[self stringValue]
asAttributedStringTruncatedToWidth:(cellFrame.size.width - 4)]];
    [super drawWithFrame:cellFrame inView:controlView];
}

@end



 _______________________________________________
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: Shouldn't dataCellForRow return an autoreleased object?
      • From: Corbin Dunn <email@hidden>
  • Prev by Date: Cannot create a foundation tool
  • Next by Date: Re: NSXMLDocument question
  • Previous by thread: Re: Cannot create a foundation tool
  • Next by thread: Re: Shouldn't dataCellForRow return an autoreleased object?
  • Index(es):
    • Date
    • Thread