[TxMt Plugins] reference counts on project NSWindows
Niko Matsakis
niko at alum.mit.edu
Sat Feb 6 09:21:09 UTC 2010
Hello,
So, I have more-or-less finished the plugin I e-mailed about earlier [1]. It inserts a WebPreview-like pane into the project window which tracks the current document. I have one problem remaining, however, which concerns memory management.
The plugin inserts a number of NSViews into the NSWindow. These NSViews have an associated controller. I would like for this controller to be deallocated at the same time that the NSWindow is deallocated. To achieve this, I am using objc_setAssociatedObject() with mode OBJC_ASSOCIATION_RETAIN to associate the object with the window; the only outstanding reference comes from this association. When the window is released, the controller should therefore be freed.
I believe that this system works fine because I have a standalone application which also uses the same plugin code, and in that case the controller is freed once the window is freed. Furthermore, if I observe the NSWindowWillClose notification, I see that my controller has only one outstanding reference (i.e., retainCount == 1) at the time that the window will close, and that the retain counts of the NSWindow are the same whether or not my preview code is installed into the window.
Unfortunately, when the plugin runs inside of TextMate, the controller is never freed. I can only assume this is because the NSWindows themselves are never freed, but that seems strange. Does anyone know a way for me to verify whether the NSWindows are being freed? Or can anyone think of a reason that this scheme would not work in TextMate? Or just a better scheme in general to achieve the same goal of having a controller that is freed when the window is freed?
Thanks in advance for any insight.
regards,
Niko
[1] Available from <http://github.com/nikomatsakis/SpanakopitaPlugin/> if you are curious.
More information about the textmate-plugins
mailing list