Re: KVO - deallocation with observers attached
Re: KVO - deallocation with observers attached
- Subject: Re: KVO - deallocation with observers attached
- From: Jonathan Mitchell <email@hidden>
- Date: Fri, 2 Jun 2017 12:59:44 +0100
> On 1 Jun 2017, at 00:51, Ken Thomases <email@hidden> wrote:
>
> On May 31, 2017, at 5:02 PM, Jonathan Mitchell <email@hidden> wrote:
>>
>> It is common for deallocating objects to abort if they have observers still registered.
>>
>> The Data_Test object is deallocated with non nil -observationInfo and yet it does not abort.
>> Why does this occur?
>
> It's not clear to me that the KVO machinery is obligated to set observationInfo to nil when an object is no longer being observed. Note that observationInfo is *not* an object pointer. It's a pointer to void. Among other things, that means that there's no memory management involved in the setter. That is, it's not a strong reference or even a weak one. So, for example, it's not necessary for KVO to nil it out to free memory.
Thanks for the input.
I wasn’t able to get to the bottom of this despite poking around with Hopper through the foundation framework.
There is an NSKVODeallocate foundation function that can get called when an observed object gets deallocated.
It raises the familiar KVO exception if -observationInfo is not nil.
- observationInfo does return a void* but it dereferences to an NSKeyValueObservationInfo : NSObject instance.
Its obviously private API though.
I was able to track the comings and goings of NSKeyValueObservationInfo using the memory graph debugger and malloc stack logging but enlightenment did not strike.
I was able to make my test project behave as I anticipated but my real world app doesn’t seem so compliant.
I am just trying to eradicate the possibility of crashes that occur when the observing object deallocs and the observed object keeps pumping notifications to the old observers address.
Thanks
J
_______________________________________________
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