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