4
votes

I have a subclass of NSWindowController like this:

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

It is the delegate of his window. Everything work OK.

But, to avoid leaking memory, should I do this in the implementation ?

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

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

Is this "the way" of doing it ? Or am I taking risk ?

Note: with Command-W the window and the controller are properly released since I'm catching this action in the AppDelegate, the one that has instantiated all this window & controller and therefore know how/when to release them. But the little red close button performClose: on it's own and the best I have achieved is to catch windowWillClose: as a window delegate...

1

1 Answers

4
votes

If you want the NSWindowController to release itself, you should use:

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

The autorelease message ensures that the close event is properly processed before releasing the NSWindowController.

Your can also check this SO entry.