Re: When is my delegate released?
Re: When is my delegate released?
- Subject: Re: When is my delegate released?
- From: James DiPalma <email@hidden>
- Date: Mon, 8 Jul 2002 04:49:27 -0400
On Sunday, July 7, 2002, at 08:43 AM, Allan Odgaard wrote:
And, in one application I have a one shot window in an external nib
file with its own controller, I haven't really grasped the correct
procedure to handle this gracefully. I've set "release when close" for
the window, so I assume that it gets released correctly when my window
close. But that doesn't take care of the delegate: I'd assume calling
release when being notified about the window closing is bad, because
there might be some last notifications sent to the delegate (like
window has now closed) -- but maybe I could just setDelegate:nil on the
window first...
Yes. I'll restate your questions answer, but you did a good job.
If you are loading a nib file on your own, none of its top level objects
will get released unless you explicitly release them; they get
explicitly retained when a nib is loaded. You can release windows by
setting "release when closed", then release its delegate (after setting
this window's delegate to nil) with a [self release] when handling this
window's willClose delegate message.
Other top level objects would have to be released by some object with
initiative since there is no clear convention for releasing top level
objects.
You could also solve this problem by loading this nib with an
NSWindowController who will gladly release all top level objects when
its window closes. There are some archived messages about toplevel
objects in nib files that might explain how NSWindowController does this.
As of such, most of my apps have their controller instantiated in the
Nib-file, but should I react on "applicationWillTerminate" or
similar, and then call release on my delegate?
You probably don't need to worry, as when your application quits all
the memory it used is automatically reclaimed.
I'm from the old school who does worry about these things... also, some
stuff like saving files, unregistering hotkeys, taking down public
NSConnections or similar really ought to be done when the application
is shutdown.
Interesting. Objects should only be released/autoreleased by objects
that explicitly retain them (or obtain an explicitly retained instance
with an init or copy method), so who "owns" a nib file's top level
objects? This question has been largely unanswered since retain/release
was introduced (7 years ago?).
I think NSApplication, who loads your main nib file is responsible for
releasing your application delegate (if it was instantiated in this main
nib). So, you could worry about releasing your delegate, but I don't
think its your responsibility.
Cocoa probably won't be changing its behavior on this issue (too many
years; we probably depend on current behavior). You can do something
similar to your answer for releasing a window's delegate (btw, I also
don't think a window's delegate should be releasing itself, but you
don't have much choice).
-jim
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.