Re: Value of self changes
Re: Value of self changes
- Subject: Re: Value of self changes
- From: Uli Kusterer <email@hidden>
- Date: Fri, 15 Nov 2013 11:09:23 +0100
On 15 Nov 2013, at 08:41, Jerry Krinock <email@hidden> wrote:
> Several years ago, I subclassed NSPopUpButtonCell and am using it as a table header cell, so the user can select which attribute is displayed in the column. Maybe this was not a good idea, but anyhow…
>
> A few years ago, I noticed a rare, unreproducible bug in my app wherein this cell would receive -drawWithFrame:inView:, but the value of self is *different* than the original value of self, which I'd stored in an instance variable during the -init of this instance. Weird! (I added this instance variable for the purpose of debugging this problem.) The cell then gets a -dealloc message, again with the “new self" shortly thereafter, and I’ve worked around the crashes which would ensue by no-opping these methods if self != the original self. It does leak, of course.
NSCell declares conformance to the NSCopying protocol. Your subclass has to, too, If you don’t, it will just do a memberwise copy of the instance variables by default, meaning you get a new instance of your class that references the same instance variable contents, but only one of them ever retained it.
Implement -copyWithZone: correctly so it copies all those values and ivars, and the problem will go away.
Cheers,
-- Uli Kusterer
“The Witnesses of TeachText are everywhere...”
http://zathras.de
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden