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

Re: Shouldn't dataCellForRow return an autoreleased object?


  • Subject: Re: Shouldn't dataCellForRow return an autoreleased object?
  • From: Corbin Dunn <email@hidden>
  • Date: Thu, 2 Mar 2006 12:38:39 -0800

On Mar 2, 2006, at 8:54 AM, Jerry Krinock wrote:

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.

Yes, that is a leak.


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

It's a different bug :)

I'll fix your code below (hand typed, in mail), and explain why.


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


@implementation MySubclassedCell

- (id)copyWithZone:(NSZone *)zone { MySubclassedCell *result = [super copyWithZone:zone]; result->_icon1 = nil; result->_icon2 = nil; [result setIcon1:_icon1]; [result setIcon2:_icon2]; return result; }

The problem: Your new cell isn't retaining its copied ivars.

Why do you have to do this?
Well, each time NSTableView goes to track or edit a cell, it copies it. It uses copyWithZone, which, by default just does an address copy of ivars. This means the dealloc will perform an extra release, and hence crash. This must be addressed in copyWithZone.


;)
corbin






_______________________________________________ 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: 
 >Shouldn't dataCellForRow return an autoreleased object? (From: Jerry Krinock <email@hidden>)

  • Prev by Date: RE: Eval() ?
  • Next by Date: Re: Commenting style
  • Previous by thread: Shouldn't dataCellForRow return an autoreleased object?
  • Next by thread: MultipleNibTabView & set of check boxes in a view
  • Index(es):
    • Date
    • Thread