• 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 14:17:19 +0100

Am 26.11.2005 um 01:17 schrieb David Gimeno Gost:
On 25 Nov 2005, at 18:57, Nick Zitzmann 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).

Actually, I think there's a different bug in this method: It violates the contract of -allocWithZone:. -allocWithZone: is guaranteed to return an object that the caller owns (i.e. has to call -release on). So, what this should really do (apart from assigning the alloc'ed object to sharedGizmoManager in the NIL case), is it should return [sharedGizmoManager retain] in both branches of the "if".


That's a lot simpler than overriding release/retain. Though out of paranoia, I'd override -dealloc so that if it gets called it NSLog()s an error message (because that never should happen for a singleton, so this is indicative of a memory error).

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

  • Prev by Date: Re: how to know the home directory and its name?
  • Next by Date: Re: retrieve a milliseconds interval with obj-C
  • 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