After binding the title of a button in a nib, I started getting the following 
burps today, whenever its window was closed.

An instance 0x100c598c0 of class MyWindowController was deallocated while key 
value observers were still registered ... [You know the drill] ... Key path: 
document.foo.bar ...

Thanks to some of the nice -description implementations given by Apple to their 
proxy things, I found the cause which was that, while this button’s title was 
bound to a different key path, ‘buttonTitle’ of MyViewController, 
MyViewController has an implementation of 
+keyPathsForValuesAffectingButtonTitle which returns 
windowController.document.foo.bar.  The window controller is being deallocced 
before the view controller.  So, although the “base” part of the binding was 
still intact, this keyPathForValuesAffecting was still in use when it was 
broken.

The view controller is instantiated in the nib.  The File’s Owner is the window 
controller, which is wired in the nib to a ‘windowController’ *outlet* of the 
view controller.  I do this because this window contains three tabs, some of 
which can take a long time to load, and to improve performance I put each tab 
in a separate nib and load it only when the user wants to see it.  Each nib 
contains its own view controller.  In order to access the data model and 
document, the view controller uses key path ‘windowController.document', via 
this outlet.  It was an architecture that I arrived at after "trying everything 
else", and I’ve been happy with it.

Well, each of my view controller’s, and my window controller, has a -tearDown 
method in which I remove all the bindings and observers that would other cause 
this kind of burp.  So I solved the problem by deleting the binding of the 
button’s title from the xib, and instead implementing the *exact* same binding 
“manually” in the view controller’s -awakeFromNib, using 
-bind:toObject:withKeyPath:options:, so that I could -unbind: it in my view 
controller’s -tearDown method.  

I’m wondering what went wrong here.  Possible explanations:

(A)  My architecture is all wrong.
(B)  You should never put an outlet in a key path.  Why not, and what 
alternative is there?
(C)  This is a very rare corner case.  Forget it and move on.
(D)  Cocoa Bindings should be smart enough to stop observing 
keyPathsForValueAffecting after being notified that a window is closing.  This 
is a bug which I should report to Apple.


_______________________________________________

Cocoa-dev mailing list ([email protected])

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to