views:

307

answers:

2

Lets say I display a window like so:

[[TBAddTaskWindowController new] showWindow:self];

Where is the TBAddTaskWindowController object meant to be released? Is the generic solution to call [self release] in windowWillClose?

It doesn't feel right to have any other object "own" the window, because it's meant to exist until the user closes it.

+1  A: 

The same code that instantiated the window controller by sending the new message to the class, just the same as if it had done it by alloc and init messages.

Peter Hosey
So, in my case, the application delegate will have to observe the window close, then release the window controller?
Tom Dalling
Yup. I'm not sure you'll get window closing notifications at app termination, though—I'm working on a blog post that's somewhat related.
Peter Hosey
A: 

Yes, a common way to do release the window controller is with:

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

The Window Controller needs to live only as long as the window is around, so autoreleasing it when the window goes away makes perfect sense.

Remember to remove any other observers, etc as well.

Peter N Lewis