Re: KVO - deallocation with observers attached
Re: KVO - deallocation with observers attached
- Subject: Re: KVO - deallocation with observers attached
- From: Ken Thomases <email@hidden>
- Date: Wed, 31 May 2017 18:51:52 -0500
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.
>
> However in some cases it seems that an object can get deallocated with observers attached and not abort.
>
> I have instrumented my test case thoroughly and overridden -setObservationInfo: to log the observation changes as below.
>
> 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.
That also means that you should not be logging the observationInfo as though it were an object (using the "%@" format specifier or otherwise sending the -description message to it). As mentioned in the observationInfo docs, "Overrides of this property must not attempt to send messages to the stored data."
By the way, does your override just log and call through to super? Or did you actually implement storage in an instance variable? If you did the latter, it's conceivable that you changed whether or not crashes are likely to follow deallocation with observers. That's because the observationInfo would not be stored in a global look-aside table such that it could get accidentally attached to an unrelated object that happened to be allocated at the same address.
> I am sorting through some observer related crash reports just trying to figure out possible causes.
I would think you'd have better luck logging the addition and removal of observers looking for an imbalance.
Regards,
Ken
_______________________________________________
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