• 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: David Gimeno Gost <email@hidden>
  • Date: Sat, 26 Nov 2005 18:21:43 +0100

On 26 Nov 2005, at 14:28, Uli Kusterer wrote:

+ (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.

Well, I didn't actually mean anything to be @synchronized(). I just copied that part of the Apple's sample code and didn't think about it. If you think there is a bug there, I suggest you file a bug report :-).


In addition, you *also* need to retain the sharedInstance you get back from allocWithZone. You need to retain once for the reference sharedInstance keeps, plus once more for the caller who called allocWithZone: and expects to get an object they need to release.

Yes, you're right, and this is indeed easily fixed, but I think the +sharedInstance method should also be modified accordingly, because in that case the shared instance variable and the instance variable the result of the allocation is assigned to are the same:


+ (id) allocWithZone: (NSZone*) zone
{
@synchronized( self ) {
if ( sharedGizmoManager == nil ) {
sharedGizmoManager = [super allocWithZone: zone];
}
}
// The +sharedInstance method assumes that an extra -retain message is
// being sent here when the singleton is first allocated, just in case the
// result of this method is assigned to another instance variable.
// Be sure to revise the +sharedInstance method if this policy changes.
return [sharedGizmoManager retain];
}


+ (MyGizmoClass*) sharedInstance
{
@synchronized( self ) {
if ( sharedGizmoManager == nil ) {
// The extra -retain in +allocWithZone: is needed only when the
// result of [[alloc] init] is assigned to a variable other than
// our static shared instance, so we undo it here.
sharedGizmoManager = [[[self alloc] init] release];
}
}
return sharedGizmoManager;
}


Regards.

_______________________________________________
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: Uli Kusterer <email@hidden>
    • Re: Is Apple's singleton sample code correct?
      • From: Uli Kusterer <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>)

  • Prev by Date: Re: Is Apple's singleton sample code correct?
  • 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