Re: static objects "autoreleased with no pool in place - just leaking"
Re: static objects "autoreleased with no pool in place - just leaking"
- Subject: Re: static objects "autoreleased with no pool in place - just leaking"
- From: "Louis C. Sacha" <email@hidden>
- Date: Fri, 13 Feb 2004 16:24:05 -0800
Hello...
Well, it's more that it _could_ potentially be called, but the
circumstances that would cause it are probably relatively rare. While
there is a general consensus that initializers are only called once
for a particular object instance, the same isn't true for
awakeFromNib...
In terms of a common concrete example of awakeFromNib being called
multiple times on a specific instance of a class, it happens in
subclasses of NSDocument. In that case, what happens is that
awakeFromNib is called for the NSDocument (or subclass) instance when
the nib is loaded, and then called again when the document is saved
using "Save as...". Of course, this is in the category of "act of a
higher power" since it is embedded in Apple's NSDocument code (at
least in 10.2), and this specific example doesn't apply to the case
of a controller in the main nib file.
In theory, it's possible that several different situations could
result in awakeFromNib to be called multiple times as a result of
your own code, although in practice it may be relatively rare that
they occur. The reason it happens is because awakeFromNib is also
called on the owner of a nib in addition to the actual archived
objects in the nib file. For example:
(1) An instance of your MyApplicationDelegate class is loaded in the
main nib, and recieves an awakeFromNib call when the nib file is
first loaded since it is an object in the nib. If your application
delegate (or any object instantiated in a nib) loads another nib file
with itself as the owner of that second nib file (perhaps because you
need to load some resources conditionally depending on whether you
are running on 10.2 or 10.3 for example), awakeFromNib is called a
second time, since it is called on the owner of the nib file as well
as the contents.
(2) An object loads multiple nibs with itself as the owner. Some
would consider this to be a problem with the design of the app,
prefering each nib should have a distinct owner which also acts as
the controller, but sometimes people have issues with using an MVC
design (since it isn't always intuitive, not always quite as easy to
apply as it seems, and sometimes forces systems to be far more
complex than they need to be...) Anyway, regardless of the reason why
the object ends up being the owner of multiple nibs, it happens in
peoples' code even though nibs aren't really designed to be used that
way, and when it does the awakeFromNib method is called once for each
nib that the object is the owner for.
There are probably other variations, but like you mentioned, if you
do a conditional check before instantiating for static variables,
that would prevent a memory leak if you wanted to use your code the
way it is in a singleton class...
Louis
... (although, in fact, even if there is only a single instance of
a particular class, awakeFromNib could potentially be called
multiple times).
Can you cite a specific instance? What could cause a Cocoa
application's main nib to be loaded multiple times (other than the
programmer reloading it explicitly)?
_______________________________________________
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.