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: Sat, 29 Aug 2009 10:29:43 +1000
Hi Andreas,
There is a strong code smell here.
How does your observee know who its observers are? Since KVO doesn't
normally provide that knowledge to the observee, you must have
arranged this yourself, possibly going considerably out of your way to
do so. Whenever you find yourself having to add great gobs of code to
"supplement" the built-in mechanism, something should start alarm
bells ringing that you are doing it wrong. And I speak from experience
here since when I first experimented with KVO, I made exactly this
mistake too.
A basic principle of KVO is that an observee is, by design, happily
unaware of who's watching it. In consequence, the only object(s) that
are responsible for stopping observing are the observers themselves -
the observee should not attempt to "chuck off" its observers.
This leads to the question of what to do when an observed object is
deallocated. Your design should have ensured that all observers have
stopped observing by then - if it makes it as far as dealloc and there
are still observers registered (as the error message you're getting
indicates) then it's too late.
This is not quite the chicken-and-egg situation it appears. Typically
an observed object is owned by another object, and that owning object
might be an observer. So whenever the owner adds or removes an object,
it would also set up/tear down the KVO observations. It's hard to give
more concrete advice without knowing more about your design, but the
bottom line is - observees cannot and should not be managing their
observers.
--Graham
On 28/08/2009, at 11:56 PM, Andreas Grosam wrote:
I'm using key-value-observing where an instance of class MyObservee
has been registered for KVO with other objects which observe a value
in a key path (e.g.: @"drives.model.port"):
The observee itself unregisters all observers in its dealloc method:
@implementation MyObservee
- (void) dealloc
{
[self removeAllObservers]; // basicly: [self
removeObserver:observer forKeyPath:key];
[super dealloc];
}
The observers are sill alive when the observee receives its dealloc
message.
When the observed instance receives its dealloc message, I'm getting
this error message in the console, before the first line of code in
the dealloc method will be executed (note: BEFORE [super deallocate]
has been invoked):
2009-08-28 14:57:49.753 MyApp[886:20b] An instance 0xd21b60 of class
MyObservee is being deallocated while key value observers are still
registered with it. Observation info is being leaked, and may even
become mistakenly attached to some other object. Set a breakpoint on
NSKVODeallocateBreak to stop here in the debugger. Here's the
current observation info:
<NSKeyValueObservationInfo 0xd38e00> (
<NSKeyValueObservance 0xd39880: Observer: 0xd356e0, Key path:
drives.model.port, Options: <New: NO, Old: NO, Prior: NO> Context:
0x16df0, Property: 0xd38990>
...
The class MyObservee does NOT have a sub class - that is, [super
dealloc] will not be called somewhere prematurely.
The base class of MyObservee is NSObject.
Am I doing something wrong here?
Thanks in advance for hints.
_______________________________________________
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