If the NSWindowController frees up in the WindowWillClose window :?

I have a subclass of NSWindowController, for example:

@interface MyController : NSWindowController <NSWindowDelegate> ... - (void)windowWillClose:(NSNotification *)notification; @end 

This is the delegate of his window. Everything is working fine.

But, to avoid a memory leak, should I do this in an implementation?

 @implementation MyController ... - (void)windowWillClose:(NSNotification *)notification { ... [self release]; } @end 

If I do not, when I close the window with the red close button, windowWillClose is called: and in Instrument I see that NSWindow is released, but not MyController ...

Is this the "way" of this? Or am I taking a chance?

Note : with Command-W, the window and the controller are correctly released, since I catch this action in AppDelegate, the one that created all this window and the controller and therefore knows how / when to release them. But the little red close button executesClose: on it, my own and the best I have achieved is to catch windowWillClose: as a window delegate ...

+4
source share
1 answer

If you want NSWindowController to be freed, you should use:

 - (void)windowWillClose:(NSNotification *)notification { [self autorelease]; } 

The autorelease message ensures that the close event is handled properly before the NSWindowController is released.

You can also check this SO> entry .

+4
source

Source: https://habr.com/ru/post/1382269/


All Articles