Observation info was leaked

2019-07-07 08:57发布

I have a class and I'm using it as an observer of itself. I need it to notify me when other objects change its properties. When it gets deallocated I don't bother to remove the observer. However I get errors like this one:

An instance 0xf819680 of class **** was deallocated while key value observers were still registered with it. Observation info was 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:

Now, I don't really get why this would be an issue. My understanding of KVO is that if object A wants to observe object B then A retains B and then

[B addObserver:A];  // shortened just to get the point across

and B does NOT retain A to avoid a cycle. Therefore, if A gets dealloced it must remove itself as an observer of B otherwise B would not have a reference to where A used to be (since it did NOT retain A).

However, this warning seems to imply that if I deallocate B something bad would happen. The only thing I can imagine happening is that the weak reference to A that B has would go away and A would just not get any more notifications. I don't know how "Observation info was leaked." If B goes away, why doesn't the observation info that it was storing go away?

1条回答
相关推荐>>
2楼-- · 2019-07-07 09:39

Actually I found out the answer to this myself. It seems that when you deallocate B it doesn't free the observation info. This is because KVO is implemented by categories (which cannot add additional data to the object itself) so the data must be kept somewhere centrally by the KVO framework.

According to the documentation for addObserver:

Neither the receiver, nor anObserver, are retained.

Therefore, when you dealloc the KVO framework doesn't know that the pointer it has to B (which it uses to compute notifications) is invalid. Therefore, if another object happens to be in that same memory space then it would become the observed object and potentially send unwanted notifications.

查看更多
登录 后发表回答