• 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: Singletons and Threads
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

References: 
 >Singletons and Threads (From: "Randall Wood" <email@hidden>)
 >Re: Singletons and Threads (From: "Clark Cox" <email@hidden>)
 >Re: Singletons and Threads (From: "Randall Wood" <email@hidden>)

  • Prev by Date: Re: Singletons and Threads
  • Next by Date: Re: NSTextView, partial views of NSTextStorage
  • Previous by thread: Re: Singletons and Threads
  • Next by thread: Re: Singletons and Threads
  • Index(es):
    • Date
    • Thread