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: Andreas Mayer <email@hidden>
- Date: Wed, 30 Nov 2005 02:23:54 +0100
Am 30.11.2005 um 01:15 Uhr schrieb David Gimeno Gost:
so, instead of sending it a -doCleanUp message, I might as well
send it a -release message.
No, you might not, since this will not necessarily lead to the
deallocation of the object. I mentioned that before.
To be honest, I really don't understand this discussion. No matter
which exact implementation you choose - if the singleton is used
as such, each one works.
No, it doesn't,
Yes, it does.
that's the whole point of the discussion. With the implementation
suggested by Apple's sample code, sending the singleton a -release
message to tell it that it's no longer needed does not work.
I wrote "if the singleton is used as such" for a reason. Sending an
object a release message does *not* ensure that the object will be
deallocated. You can't expect a singleton to be deallocated when you
think it should be. It might be, but it need not.
See <http://www.cocoabuilder.com/archive/message/cocoa/
2005/11/28/151235> for an example of why this could be a problem.
If it won't work for a specific case, then use a pattern that better
fits the problem.
And there are other nasty side effects too, such as that you must
write the -init method to take into account the possibility that it
gets invoked more than once for the same object.
With most classes that provide singletons you are not supposed to
create other objects of that class (and therefore should not send
them an init message). This is usually mentioned in the class
documentation.
Note that the sample code provided by Apple will likely be copied
and used by many novices without really understanding its nasty
side effects
I'm still not convinced there are any.
Since I find this discussion tiring and not very productive, I'll
stop here. Do what you think is best.
Andreas
_______________________________________________
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