• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Is Apple's singleton sample code correct?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is Apple's singleton sample code correct?


  • Subject: Re: Is Apple's singleton sample code correct?
  • From: Marco Scheurer <email@hidden>
  • Date: Tue, 29 Nov 2005 00:35:24 +0100

On Nov 28, 2005, at 21:38, David Gimeno Gost wrote:

On 28 Nov 2005, at 20:29, glenn andreas wrote:

Objective-C does not have "Destructors".

Exactly. It's bad enough that Objective-C does not provide such facilities. Lets at least adhere to the conventions of the memory management provided by the framework, whether singleton or not. Doing that does not prevent the singleton from being a singleton.

We've heard that many times from C++ control freaks, about static typing, the lack of a language construct for an abstract class, etc.


But I believe that David is right at the core, in that the quoted code:

+ (id)allocWithZone:(NSZone *)zone
{
    @synchronized(self) {
        if (sharedGizmoManager == nil) {
            return [super allocWithZone:zone]; // -> Is this correct?
       }
    }
    return sharedGizmoManager;
}

is buggy, and not just because of a missing assignment.

In Cocoa you advertize a singleton by providing a method that conveys this meaning: +sharedInstance, +defaultManager, etc. However, I don't see why alloc should be twisted around to change its meaning. If your client code ignores your singleton creating methods, and decide to blindly use alloc and init, then so be it. It may not work but that's not worse, for instance, than using init instead of a designated initializer in another case. It does not matter: you cannot protect yourself against every stupid things that your code users can do, and you should not.

So not only is an assignment to sharedGizmoManager missing, despite the air of superiority coming from the use of @synchronized this code is buggy because it breaks an important convention. A more useful singleton implementation was given by joar:

+ (MySingletonClass *) sharedInstance
{
	static MySingletonClass *mySharedInstance = nil;
	if (nil == mySharedInstance)
	{
		mySharedInstance = [[MySingletonClass alloc] init];
	}
	return mySharedInstance;
}

AFAIK, this is what has been used again and again for years.

Singleton is not a language feature, not a framework feature, but a convention. This code is not part of a framework, it is not something you inherit from in order to get a singleton, it is just an example of how to program one (you can't inherit from that code because of the limitation coming from the use of a static variable).

It is simple and works in 99% of cases. What's wrong with that?

If it does not work for you, then write your own that can be released, and in addition to freeing memory will release whatever resources are used. It could be good or bad design, but you know what needs to be done. And if your clients code honors the memory management rules, your singleton should end up being deallocated, because contrary to the quoted example from Apple's conceptual doc (!) this code does not break the memory management rules.

marco


Marco Scheurer Sen:te, Lausanne, Switzerland http://www.sente.ch

_______________________________________________
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


References: 
 >Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: Shawn Erickson <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: Uli Kusterer <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: Uli Kusterer <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: mmalcolm crawford <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: mmalcolm crawford <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: Christian Brunschen <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: glenn andreas <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)

  • Prev by Date: Correctly using hidden NSWindow to render offscreen OpenGL Context
  • Next by Date: Re: Is Apple's singleton sample code correct?
  • Previous by thread: Re: Is Apple's singleton sample code correct?
  • Next by thread: Re: Is Apple's singleton sample code correct?
  • Index(es):
    • Date
    • Thread