Re: Get error message about registered observers when Object receives dealloc message
Re: Get error message about registered observers when Object receives dealloc message
- Subject: Re: Get error message about registered observers when Object receives dealloc message
- From: Graham Cox <email@hidden>
- Date: Sun, 30 Aug 2009 13:24:57 +1000
On 30/08/2009, at 12:35 PM, Roland King wrote:
Now this I don't understand. If you enter your dealloc method with
observers registered on yourself and do have a *safe* mechanism of
ensuring they are completely removed before ending the dealloc
routine (or calling [ super dealloc ] ) I don't see where the issue
should lie. Can you point me to the note on that please too. I
suppose if you did permit this you're sending a message to an object
whilst it's being deallocated however the KVO registration methods
are supplied by the NSObject superclass, who's dealloc method has
not yet been called.
I believe the issue lies in the fact that an object that has KVO
observers registered on it is not the object you think it is. It's in
fact a sort of proxy for the "real" object that has been swizzled (you
can see this in the debugger as the class will be
KVONotifying_Whatever). When the last observer is removed it's
swizzled back to the original class. The question would be just how
possible/safe this would be to do in -dealloc.
Nevertheless, I think it's a moot point. Apple have decreed that
removing observers in dealloc is too late; it flags a fairly stern
warning about doing it - a warning that got much more strongly worded
between 10.4 and 10.5. So whether or not there could be a way to do it
in theory, in practice you have to find a way to do it some other way.
Personally I found that once I grokked this (and it did take a while)
it started to inform the design of other parts of my data model, in
that knowing I planned to use KVO meant that I would build-in proper
mechanisms for observing and de-observing at the right places in a
legal fashion. As a result I went from finding KVO a pain in the butt
to being one of the best things about Cocoa.
One way or another, I think Andreas has to re-jig his design.
--Graham
_______________________________________________
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