• 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: Nick Zitzmann <email@hidden>
  • Date: Fri, 25 Nov 2005 10:57:47 -0700


On Nov 25, 2005, at 10:14 AM, David Gimeno Gost wrote:

1. I believe there is a bug in the following class method:

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

No, that's correct. It prevents +allocWithZone: from creating a new instance of the object if the singleton has already been created (sharedGizmoManager is not nil).


4. What's the point in overriding the -retain, -release, and - autorelease methods?

So the singleton can never be deallocated.

As written in the example, these methods will silently allow the programmer to write buggy code that doesn't follow Cocoa's memory management rules.

No they won't. Singletons are not supposed to be deallocated under any circumstances.


5. Does allowing calling -copy and -copyWithZone: on singletons make sense?

No, but that doesn't stop people from trying it anyway.

Nick Zitzmann
<http://www.chronosnet.com/>

_______________________________________________
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: David Gimeno Gost <email@hidden>
References: 
 >Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)

  • Prev by Date: Re: Drawing crash - not near my code!
  • Next by Date: Re: Is Apple's singleton sample code correct?
  • Previous by thread: Is Apple's singleton sample code correct?
  • Next by thread: Re: Is Apple's singleton sample code correct?
  • Index(es):
    • Date
    • Thread