potential leak warnings from static analyzer on window controllers


James Walker
 

The static analyzer gives me some warnings of possible leaks that I know aren't real leaks, but it makes me think that I may be using a bad design pattern.  The situation is that I create an instance of a subclass of NSWindowController, and when the window closes, the controller releases itself.  The static analyzer reports a possible leak at the point of creation.  This window is not associated with an NSDocument, and it is non-ARC code.  What should I be doing differently?


Quincey Morris
 

On Dec 7, 2017, at 09:32 , James Walker <list2@...> wrote:

What should I be doing differently?

You could create a persistent reference. If your window controller will have only one instance (such as a non-document app’s main window), you can have a simple static variable in (say) the window controller class’s implementation file. If there are multiple window controllers, you can add the references to a global array in (say) the app delegate (and do a release after adding, to keep the retain count balanced).

Then, when the window closes, you will release and nil the static variable, or remove the reference from the array, rather than doing the [self release].


James Walker
 

On 12/7/2017 11:29 AM, Quincey Morris wrote:
On Dec 7, 2017, at 09:32 , James Walker <list2@...> wrote:

What should I be doing differently?

You could create a persistent reference. If your window controller will have only one instance (such as a non-document app’s main window), you can have a simple static variable in (say) the window controller class’s implementation file. If there are multiple window controllers, you can add the references to a global array in (say) the app delegate (and do a release after adding, to keep the retain count balanced).

Then, when the window closes, you will release and nil the static variable, or remove the reference from the array, rather than doing the [self release].

Thanks for the ideas!  I ended up using a global NSMutableSet.