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: 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