Re: Singletons and Threads
Re: Singletons and Threads
- Subject: Re: Singletons and Threads
- From: PGM <email@hidden>
- Date: Fri, 14 Sep 2007 21:49:55 -0400
+ (MPInterpreter*)sharedInterpreter {
@synchronized(self) {
if (sharedMPInterpreter == nil) {
[[self alloc] init]; // assignment not done here
}
}
return sharedMPInterpreter;
}
You are not assigning the newly inited object to the
sharedMPInterpreter, so you are in fact returning nil. Leaving aside
the @synchronized bit, I would have written it like this:
+ (MPInterpreter *) sharedInterpreter
{
static MPInterpreter *sharedInstance = nil;
if (sharedInstance == nil)
{
sharedInstance = [[[self class] alloc] init];
}
return sharedInstance;
}
Cheers, Patrick
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden