Bindings and File's Owner
There's an issue with Cocoa bindings that a number of people keep running into. Here's the short and simple:If you have a nib in which File's Owner is a subclass of NSWindowController or NSDocument, do not bind anything through file's owner.
The problem is that bindings that go through File's Owner cause parents and children to retain each other, causing a memory leak that grows with each new window.
Dennis C. De Mars did some investigation into this, and came up with the simplest solution I've seen thus far:
- Create an NSObjectController
- Populate it after the nib wakes up using -setContent:
- Bind UI elements through the NSObjectController instead of File's Owner
I believe Apple is aware of the issue, but a workaround is necessary right now.
Bindings and File's Owner
Posted Oct 11, 2004 — 11 comments below
Posted Oct 11, 2004 — 11 comments below
Dan Wood — Dec 20, 04 45
http://www.cocoabuilder.com/archive/message/2004/7/28/113081
Scott Stevenson — Dec 22, 04 46
Steven Kramer — Jan 10, 05 59
Dan Stein — Feb 09, 05 83
Ricardo — May 03, 05 151
Thanks for the articles about CoreData, just printed them and will have some night reading to do tonight...
I was wondering if you have some tutorials on NSController and NSArrayController
I read some stuff at apple and even play with them but i think i am missing the deep down idea on how to use them.
Thanks
Ricardo
Ricardo — May 03, 05 152
Matt Gallagher — Dec 12, 05 608
This doesn't always work (quitting an application with a document window open that has no unsaved changes results in the document not getting unbound -- work around is to closeAllDocumentsWithDelegate manually on applicationShouldTerminate) and still doesn't fix the problem when the owner is neither an NSDocument nor NSWindowController.
In these "doesn't always work" situations, the NSObjectController fix described above still works.
Just so you know.
Jamie Kirkpatrick — Mar 20, 06 943
In your NSWindowController subclass override awakeFromNib: and release with something that looks like this:
- (void) awakeFromNib;
{
// filesOwnerProxy == NSObjectController in the nib
[filesOwnerProxy setContent:self];
}
- (oneway void) release;
{
// special case when count is 3, we are being retained twice by the object controller...
if ( [self retainCount] == 3 )
{
[super release];
[filesOwnerProxy setContent:nil];
return;
}
[super release];
}
When you setContent: in the NSObjectController you get two extra retains in the new contentObject. This is a nice solution since you can be sure that the extra two retains will be from your objectController, and you can safely bind as much as you like to that controller. The only issue will be if apple changes their NSObjectController etc code.
Bruce Rakes — Feb 06, 07 3510
Does anyone know how Leopard and garbage collection will affect this?
Scott Stevenson — Feb 06, 07 3513
Garbage collection should make it a moot point.
Christiaan Hofman — Aug 23, 08 6305
1. Binding to the File's Owner with key path "self" (this occurs when you use an NSObjectController 'proxy' for the File's Owner, e.g. to have a funnel point for NSEditor/NSEditorRegistration, as is recommended by many reputable people and the docs).
2. Binding an NSArrayController's contentArray to a to-many property of the File's Owner for which only indexed KVC accessors are implemented (i.e., not -<key> and -set<Key>:). Note that this is actually recommended practice (at least according to some people).