drawing NSTextFieldCell nested inside NSTextFieldCell subclass causes crash

  • I have an NSTextField subclass. Its function is to provide a cell
    with two vertically stacked text areas in one cell.

    Things are generally working ok, its being instantiated, put into a
    column in an NSOutlineView. I've shifted the default text area down
    18.0 pixels, to leave space for the upper textfield. This is an
    NSTextFieldCell, stored in an instance variable called titleField in
    the cell. This is being created on init of the cell, and that's
    confirmed using NSLog.

    When I try to draw the extra cell, I use this:

    // -----------------------------
    - (void) drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
    NSSize titleSize;
    NSRect titleFrame;

    titleSize.width = 300.0;
    titleSize.height = 18.0;

    NSDivideRect
    (cellFrame,&titleFrame,&cellFrame,titleSize.height,NSMinYEdge);

    NSLog(@"frames: \rcell x: %f y: %f w:%f h:%f \r\r title: x: %f y: %f
    w:%f h:%f",
          cellFrame.origin.x, cellFrame.origin.y, cellFrame.size.width,
    cellFrame.size.height,
          titleFrame.origin.x, titleFrame.origin.y,
    titleFrame.size.width, titleFrame.size.height);
    if ( titleField != nil) {
      [titleField drawWithFrame:titleFrame inView:controlView];
    }
    [super drawWithFrame:cellFrame inView:controlView];
    }

    and the application crashes. I'm not very experienced with Cocoa, but
    this seems reasonable. The frames are both there, but it isn't
    working...nothing has been done with titleFrame except alloc/init/
    autorelease. cellSize is being used to manipulate the row height
    correctly.

    Help (please)...

    Dan Donaldson
    416 604 3398
    <dan...>
  • Have you properly implemented your new cell's copyWithZone: method?
    NSCells get copied a lot when being drawn.  If you don't implement
    this, the table view will copy your cell incorrectly and screw up
    your memory management.

    Mike.

    On 19 Oct 2006, at 21:13, Dan Donaldson wrote:

    > I have an NSTextField subclass. Its function is to provide a cell
    > with two vertically stacked text areas in one cell.
    >
    > Things are generally working ok, its being instantiated, put into a
    > column in an NSOutlineView. I've shifted the default text area down
    > 18.0 pixels, to leave space for the upper textfield. This is an
    > NSTextFieldCell, stored in an instance variable called titleField
    > in the cell. This is being created on init of the cell, and that's
    > confirmed using NSLog.
    >
    > When I try to draw the extra cell, I use this:
    >
    > // -----------------------------
    > - (void) drawWithFrame:(NSRect)cellFrame inView:(NSView *)
    > controlView {
    > NSSize titleSize;
    > NSRect titleFrame;
    >
    > titleSize.width = 300.0;
    > titleSize.height = 18.0;
    >
    > NSDivideRect
    > (cellFrame,&titleFrame,&cellFrame,titleSize.height,NSMinYEdge);
    >
    > NSLog(@"frames: \rcell x: %f y: %f w:%f h:%f \r\r title: x: %f y: %
    > f w:%f h:%f",
    > cellFrame.origin.x, cellFrame.origin.y, cellFrame.size.width,
    > cellFrame.size.height,
    > titleFrame.origin.x, titleFrame.origin.y,
    > titleFrame.size.width, titleFrame.size.height);
    > if ( titleField != nil) {
    > [titleField drawWithFrame:titleFrame inView:controlView];
    > }
    > [super drawWithFrame:cellFrame inView:controlView];
    > }
    >
    > and the application crashes. I'm not very experienced with Cocoa,
    > but this seems reasonable. The frames are both there, but it isn't
    > working...nothing has been done with titleFrame except alloc/init/
    > autorelease. cellSize is being used to manipulate the row height
    > correctly.
    >
    > Help (please)...
    >
    > Dan Donaldson
    > 416 604 3398
    > <dan...>
    >
    >
    >
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/mike.abdullah%
    > 40gmail.com
    >
    > This email sent to <mike.abdullah...>
  • This is the implementation I used:

    - copyWithZone:(NSZone *)zone {
    DDDoubleTextFieldCell * cell = (DDDoubleTextFieldCell *)[super
    copyWithZone:zone];
    cell->cellTitle = [cellTitle retain];
    cell->titleField = [titleField retain];
    return cell;
    }

    however, the line cell->titleField = [titleField retain];  causes a
    crash for reasons not clear to me.

    Dan Donaldson
    416 604 3398
    <dan...>

    On 20-Oct-06, at 6:44 AM, Mike Abdullah wrote:

    > Have you properly implemented your new cell's copyWithZone:
    > method?  NSCells get copied a lot when being drawn.  If you don't
    > implement this, the table view will copy your cell incorrectly and
    > screw up your memory management.
    >
    > Mike.
    >
    > On 19 Oct 2006, at 21:13, Dan Donaldson wrote:
    >
    >> I have an NSTextField subclass. Its function is to provide a cell
    >> with two vertically stacked text areas in one cell.
    >>
    >> Things are generally working ok, its being instantiated, put into
    >> a column in an NSOutlineView. I've shifted the default text area
    >> down 18.0 pixels, to leave space for the upper textfield. This is
    >> an NSTextFieldCell, stored in an instance variable called
    >> titleField in the cell. This is being created on init of the cell,
    >> and that's confirmed using NSLog.
    >>
    >> When I try to draw the extra cell, I use this:
    >>
    >> // -----------------------------
    >> - (void) drawWithFrame:(NSRect)cellFrame inView:(NSView *)
    >> controlView {
    >> NSSize titleSize;
    >> NSRect titleFrame;
    >>
    >> titleSize.width = 300.0;
    >> titleSize.height = 18.0;
    >>
    >> NSDivideRect
    >> (cellFrame,&titleFrame,&cellFrame,titleSize.height,NSMinYEdge);
    >>
    >> NSLog(@"frames: \rcell x: %f y: %f w:%f h:%f \r\r title: x: %f y:
    >> %f w:%f h:%f",
    >> cellFrame.origin.x, cellFrame.origin.y,
    >> cellFrame.size.width, cellFrame.size.height,
    >> titleFrame.origin.x, titleFrame.origin.y,
    >> titleFrame.size.width, titleFrame.size.height);
    >> if ( titleField != nil) {
    >> [titleField drawWithFrame:titleFrame inView:controlView];
    >> }
    >> [super drawWithFrame:cellFrame inView:controlView];
    >> }
    >>
    >> and the application crashes. I'm not very experienced with Cocoa,
    >> but this seems reasonable. The frames are both there, but it isn't
    >> working...nothing has been done with titleFrame except alloc/init/
    >> autorelease. cellSize is being used to manipulate the row height
    >> correctly.
    >>
    >> Help (please)...
    >>
    >> Dan Donaldson
    >> 416 604 3398
    >> <dan...>
    >>
    >>
    >>
    >> _______________________________________________
    >> Do not post admin requests to the list. They will be ignored.
    >> Cocoa-dev mailing list      (<Cocoa-dev...>)
    >> Help/Unsubscribe/Update your Subscription:
    >> http://lists.apple.com/mailman/options/cocoa-dev/mike.abdullah%
    >> 40gmail.com
    >>
    >> This email sent to <mike.abdullah...>
    >
previous month october 2006 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today