Re: Is Apple's singleton sample code correct?
Re: Is Apple's singleton sample code correct?
- Subject: Re: Is Apple's singleton sample code correct?
- From: Scott Ellsworth <email@hidden>
- Date: Tue, 29 Nov 2005 11:26:28 -0800
On Nov 29, 2005, at 7:49 AM, glenn andreas wrote:
On Nov 29, 2005, at 9:02 AM, Darkshadow wrote:
-dealloc is not guaranteed to be called when the application is
quitting. So if you have resources (other than memory) that need
to be cleaned up, you should really listen for the -
applicationWillTerminate: notification and do the clean up there.
I don't see this as such a big surprise - dealloc happens when the
last reference to an object is made is removed (i.e., "release" is
called). So when you quit the application, what is releasing that
last reference? You expect something to magically go through all
global variables, determine if they are pointing to something that
responds to "release" (and hasn't already been released) and send
that message?
Delete the word 'magical', and this was exactly what I expected when
I first learned Cocoa. I was used to runtimes that did this - Java
and C++. I got used to it when doing Cocoa, and a good thing, as
recent JVMs no longer do this either.
In other words, it is a behavior to document well, learn, and move
on. Not a religious principle, but a design deep inside Cocoa.
This design decision can be legitimately made either way. The
'objects are always deleted when their life is over' model is very
clean from an OO perspective, but has nasty runtime implications when
you shut down a process. Zillions of objects being carefully
dealloced and memory added to free lists, just so the whole thing can
be thrown away a few milliseconds later is a big waste of time,
unless you were expecting to do something when your object dies.
As you say - Cocoa is a library, and thus it has standard behaviors.
When I first learned Cocoa, I expected the standard behavior to be
'everything is dealloced at some point', and was surprised when I
found out that it used a notification instead. I then expected the
standard singleton to be hooked into that quit notification. and was
surprised when it was not. More careful reading of the docs before
coding might have prevented both surprises.
There is no a priori reason why it has to be this way, but this way
does work. As I said above, Java has had both behaviors during its
lifetime, and Java programmers dealt with it.
Scott
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden