Quincey is correct that closing of windows in macOS is a complicated, tricky 
process which may have edge case bugs.  Also, although you mentioned invoking 
-bind: (also known as “manual” binding), you have not mentioned -unbind:.

In complicated window controllers, I implement a -tearDownOnce method which is 
invoked by -[NSWindowControllerDelegate windowWillClose] and also by 
-[NSDocumentController removeDocument].  After checking that it has not been 
run, -tearDownOnce invokes -unbind: on anything which I have “manually” bound.

I started doing this many years ago.  I think I remember reading somewhere that 
such *unbinding* is no longer necessary in recent versions of macOS.  Does 
anyone know of reference on that?  I just looked at the documentation on this, 
Cocoa Bindings Programming Topics > Unbinding, and found this:

# # # #

Typically the only reason you would explicitly unbind an object is if you 
modify the user interface programatically and want to remove a binding. If you 
change an objects binding’s values it should first clear any preexisting values.

If you implement a custom view or controller with custom bindings, you should 
ensure that it clears any bindings before it is deallocated—in particular it 
should release any objects that were retained for the specified binding in the 
bind:toObject:withKeyPath:options: method and should deregister as an observer 
of any objects for which it registered as an observer. It may be convenient to 
implement this logic in an unbind: method that you then call as the first step 
in dealloc.

# # # #

I’m confused.  The first paragraph says not to worry about it, and the second 
says, well, maybe you do.  So, whether unbinding is really necessary nowadays 
or just defensive programming, I don’t know.  But I’ve found that as long as I 
-unbind early, as I described above, and ensure that I carefully balance -bind: 
and -unbind:, I don’t get those pesky NSAutoUnbinder crashes.

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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 arch...@mail-archive.com

Reply via email to