• 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: Uli Kusterer <email@hidden>
  • Date: Sat, 26 Nov 2005 18:02:22 +0100


Am 26.11.2005 um 16:00 schrieb Jim Correia:

On Nov 26, 2005, at 8:28 AM, Uli Kusterer wrote:

Am 26.11.2005 um 04:50 schrieb David Gimeno Gost:
+ (id) allocWithZone: (NSZone*) zone
{
    @synchronized( self ) {
        if ( sharedInstance == nil ) {
            sharedInstance = [super allocWithZone: zone];
        } else {
            [sharedInstance retain];
        }
    }
    return sharedInstance;
}

Errm ... good start, but you meant to be @synchronized() on some other object. If you synchronize to self, two threads create a "self", and both sync to their own "self"s, so you effectively get the same result as if you didn't have the @synchronized call in there at all. You need an explicit NSLock here, or one shared object that you know you can rely on already existing.

self here is the class object, which already exists, and is global.

Ah, right. Sneaky little "+" sign. :-D

But isn't that dangerous? That would mean that calling alloc/init on such an object would repeatedly call init on the same object :-(

Cheers,
-- M. Uli Kusterer
http://www.zathras.de


_______________________________________________ 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
  • Follow-Ups:
    • Re: Is Apple's singleton sample code correct?
      • From: Shawn Erickson <email@hidden>
    • Re: Is Apple's singleton sample code correct?
      • From: Jim Correia <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: Jim Correia <email@hidden>)

  • Prev by Date: Getting NSDictionary's dictionaryWithContentsOfURL to honor the user's HTTP Proxy setting
  • Next by Date: [ANN] MKAMRollOverExtensions 0.2
  • 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