English 中文(简体)
How does NSViewController avoid bindings memory leak? [have sample app]
原标题:

I m trying to implement my own version of NSViewController (for backwards compatibility), and I ve hit a problem with bindings: Since bindings retain their target, I have a retain circle whenever I bind through File s owner.

So I thought I d just explicitly remove my view from its superview and release the top level objects, and that would take care of the bindings, because my controller isn t holding on to the views anymore, so they release me and I can go away. But for some reason, my view controller still doesn t get released. Here s a sample app exhibiting the problem:

http://dl.dropbox.com/u/34351/BindingsLeak.zip

Build it, launch it, and hit Cmd-K ("Create Nib" in "Edit" menu) to load a NIB into the empty window. Hit Cmd-K again to release the first view controller (TestNibOwner) and load a new one. The old view controller never gets dealloced, though.

Remove the "value" binding on the checkbox, and it gets released just fine.

If you set breakpoints at the release/retain/autorelease overrides, you see that _NSBindingInfo retains the TestNibOwner, but never releases it in the leaking case.

Anyone know how to fix this?

最佳回答

One thing I ve done for the same problem is to create a proxy NSObjectController inside my nib. My NSViewController-like class has a pointer to this proxy and all bindings are bound through it. When I want to cleanup the view controller, I then do [selfProxy setContent:nil] on the object controller and release the view controller. In this instance the NSObjectController proxy acts as the auto-unbinder in this case.

It s more manual and you can t just release the view by itself, but it does solve the retain problem.

I d suggest you do this:

-(void) releaseTopLevelObjects
{
    // Unbind the object controller s content by setting it to nil.
    [selfProxy setContent:nil];

    NSLog( @"topLevelObjects = %@", topLevelObjects );
    [topLevelObjects release];
    topLevelObjects = nil;
}

In your nib, bindings would happen through a path like:

selfProxy.content.representedObject.fooValue
问题回答

Doing a little investigation with class-dump and friends, it looks like Apple has a private class called NSAutounbinder that takes care of this dirty work for classes such as NSViewController and NSWindowController. Can t really tell how it works or how to replicate it though.

So, I can t really answer your question on how to prevent the retain cycle from happening for arbitrary bindings in a loaded nib, but perhaps it s some consolation to know that Apple is cheating, and you re not missing anything obvious. :-)

When you remove your view from its superview, are you also sending it another -release message? It was created by unarchiving from the nib, right?





相关问题
Weak event handler model for use with lambdas

OK, so this is more of an answer than a question, but after asking this question, and pulling together the various bits from Dustin Campbell, Egor, and also one last tip from the IObservable/Rx/...

JavaScript memory problem with canvas

I m using getImageData/putImageData on a HTML5 canvas to be able to manipulate a picture. My problem is that the browser never seems to free any memory. Not until I close the tab (tested in Chrome and ...

Memory leak for CComBSTR

I have read that the following code causes memory leak. But did not understand why. CComBSTR str; pFoo->get_Bar(&str); pFoo->get_Baf(&str); How does it cause a leak when we are not ...

Hunting memory leaks

I m finding leaked heap blocks by using the following command in WinDbg !heap –l With each leaked heap block I get, I m running to following to get the stack trace. !heap -p -a leakedheapblock The ...

NSScanner memory leak

I m at my first experiences with iPhone development. I wrote some basic code to test the NSScanner class, and now I was looking into the Leaks tool. It seems that this code is leaking, when in the ...

Do Small Memory Leaks Matter Anymore?

With RAM typically in the Gigabytes on all PC s now, should I be spending time hunting down all the small (non-growing) memory leaks that may be in my program? I m talking about those holes that may ...

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

热门标签