Re: Singletons and Threads
Re: Singletons and Threads
- Subject: Re: Singletons and Threads
- From: "Shawn Erickson" <email@hidden>
- Date: Fri, 14 Sep 2007 14:05:51 -0700
On 9/14/07, Randall Wood <email@hidden> wrote:
> This is the code I have for ensuring that the class is a singleton:
>
> static MPInterpreter *sharedMPInterpreter = nil;
>
>
> @implementation MPInterpreter
>
>
> - (id) init {
>
> if (self = [super init]) {
>
> _interpreter = Tcl_CreateInterp();
>
> if(_interpreter == NULL) {
>
> NSLog(@"Error in Tcl_CreateInterp, aborting.");
>
> return Nil;
>
> }
Your error pathways in your init method will leak the instance being
inited. If you are going to return nil you need to send yourself a
release message first.
Also nothing in the code posted ensures that only one will exist in a
given thread. To do that consider using -[NSThread threadDictionary]
to store a reference to the thread scoped instance.
-Shawn
_______________________________________________
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