Re: KVO cross dependencies
Re: KVO cross dependencies
- Subject: Re: KVO cross dependencies
- From: Roland King <email@hidden>
- Date: Fri, 13 Nov 2009 22:00:20 +0800
One option - use Garbage Collection under Snow Leopard, it does it all for you.
If you can't do that. I assume, from the fact that your observed objects are being released at all that the observing objects are not retaining them (or keeping strong references if you are using Garbage Collection), if you had them retained by the observing object then they wouldn't be getting dealloced. If the observations are in a tree, then you could have the observers retain the objects they are observing and unobserve them as they release them, then you'll never have the issue of an object with registered observers being deallocated.
If, however you can't do that, because the objects all observe each other, you'll get retain cycles (again assuming you are not using GC), so you cannot retain and need to tell the observing object to remove its observations on the observed object before it dies. One way to do that is to have another property on the objects, something like canBeObserved, and have all your observers observe that property *as well* as other observations they have. In your dealloc method, right at the start of it, flip that property to NO and all your observers will be told you are no longer observable, they are then responsible for removing their observations. This happens synchronously, so by the time you continue with dealloc, nobody is observing you any more.
We had a thread about this a few months ago where we discussed whether it was legitimate to do this in dealloc and discussing a note in the Snow Leopard release notes about that message and I believe that most people were satisfied that it was a supported thing to do. Note that even if you do this, under 10.5 you will STILL get that warning message (not an exception) because it was emitted at the start of dealloc() before you had a chance to tell your observers to stop observing, that was what the release note addressed.
I don't think there is a documented way of finding out who is observing you and what they are observing.
On 13-Nov-2009, at 9:43 PM, Half Activist wrote:
> Hi all,
>
> I have sevral objects of different kind that observe values on each other.
> Now, when the objects are being released, I get an exception from the KVO stuff telling me that an object is being deallocated while still observed. And that's justified. Yet, the problem is in whatever way, order I deallocate the objects that will always happen because of these cross observations. Therefore what I should do is tell everyone that's observing an object to remove them as observers. But how can I get a list of all observed keys and the observers?
>
> Regards.
>
> PS: I thought about setting the observation info to nil but I guess this is a one way ticket to a segfault.
>
> _______________________________________________
>
> 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
_______________________________________________
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