Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

KVO fails when hash overridden

#223

If two objects override hash to return the same value (either on purpose to indicate equality or by accident) and observers are set on both objects, the KVO mechanism treats them as the same object.

This can result in, for example, an observer getting notifications for properties it didn't set observers for.

See the attached test case for an example.

This appears to be due to KVOProxyMap using the object hash as the lookup key.

Perhaps there needs to be some kind of non-overrideable id property available on CPObject which is guaranteed to be unique and should be used in these cases instead of hash?

I believe there is a similar issue in CPCoding where two objects with the same hash can confuse things. I haven't worked up a test case for that one (yet).

An additional issue is that calling [self class] within hash on an observed object causes a stack overflow due to infinite recursion. This is due to _CPKVOModelSubclass class calling [self hash] but if a non-overrideable id property was used instead this should go away.

Reported by maport · February 19th, 2009 @ 03:48 AM

State: new
Milestone: none
Assigned to: nobody

Activity

No comments yet.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.