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: Andy Lee <email@hidden>
- Date: Mon, 28 Nov 2005 21:06:25 -0500
On Nov 28, 2005, at 6:51 PM, David Gimeno Gost wrote:
On 28 Nov 2005, at 22:20, Ondra Cada wrote:
Oh sigh...
Agreed. :-)
I haven't seen one of Ondra's sighs on the list for a while. Glad to
see he's alive and well. :)
Cripes, what a long thread. I've lost track of who favors what, but
FWIW, I'm for not mucking with the strong conventional semantics of
+allocWithZone:. I'd go with the simple +sharedInstance
implementation using a local static variable, as illustrated numerous
times on this thread, and stick with normal retain/release rules
throughout your application.
Put resource cleanup inside the object's -dealloc method, as you
would for a non-singleton. Then if you want to explicitly force
cleanup, you can send one last -release message in response to the
app-will-end notification. I don't think this is so evil. It's an
additional convention (the final -release, that is) rather than a
special casing of an existing one.
If you really want to discourage instances other than the shared
instance (and maybe "shared instance" is a better term than the
overloaded "singleton" in this context), you could have
+allocWithZone: and -init throw exceptions. I think this is more
appropriate than trying to have them silently paper over the fact
that they were called when they shouldn't be.
--Andy
_______________________________________________
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