• 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: Global in NSApplication
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Global in NSApplication


  • Subject: Re: Global in NSApplication
  • From: Jens Alfke <email@hidden>
  • Date: Wed, 27 Jan 2010 09:29:45 -0800


On Jan 27, 2010, at 8:37 AM, Scott Ribe wrote:

Well, even simpler, you could just use C without fancy singletons, as in:

Debug *gDebug = nil;

int main(int argc, char *argv[])
{
   gDebug = [[Debug alloc] init];
   return NSApplicationMain(argc,  (const char **) argv);
}

Depending on what gDebug does, this could be unsafe. It would be a bad idea to make any AppKit calls before NSApplicationMain, for example. This also makes it difficult to use the Debug class in a framework/ library.


It can also be tempting to initialize gDebug in something like the Debug class's +initialize method; but this is still dangerous because something might access gDebug before calling any class methods on Debug. (The use of a +load method would fix this, but that's discouraged because it slows down launch.) I had an issue like this in an app I wrote once, and we kept running into bugs where some code happened to access gMe before it had been initialized. In hindsight I really should have just made it a +sharedInstance method instead of a global variable.

—Jens_______________________________________________

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


  • Follow-Ups:
    • Re: Global in NSApplication
      • From: Scott Ribe <email@hidden>
References: 
 >Re: Global in NSApplication (From: Scott Ribe <email@hidden>)

  • Prev by Date: Re: Context menu on NSOutlineView, which row is active/selected?
  • Next by Date: NSTask and uudecode
  • Previous by thread: Re: Global in NSApplication
  • Next by thread: Re: Global in NSApplication
  • Index(es):
    • Date
    • Thread