Re: Global in NSApplication

2010-01-27 Thread Scott Ribe
> Ah, that's different. I understood you to mean that it ALWAYS created multiple > instances. No, sorry, I only meant that it did not guarantee a single instance. The point I was (obtusely) attempting to make was: do not complicate things with a stronger-than-necessary guarantee; take into account

Re: Global in NSApplication

2010-01-27 Thread Graham Cox
On 28/01/2010, at 8:51 AM, Scott Ribe wrote: > The code you posted has a race condition, and can create multiple instances > when called from different threads. Ah, that's different. I understood you to mean that it ALWAYS created multiple instances. Any threaded programming requires a lot of

Re: Global in NSApplication

2010-01-27 Thread Kyle Sluder
On Wed, Jan 27, 2010 at 1:51 PM, Scott Ribe wrote: > The code you posted has a race condition, and can create multiple instances > when called from different threads. This is true. My favorite way of avoiding it: + (Foo *)sharedFoo { static Foo *instance; static dispatch_once_t once; dispa

Re: Global in NSApplication

2010-01-27 Thread Scott Ribe
> Calling a singleton method from multiple threads does not make multiple > instances. The code you posted has a race condition, and can create multiple instances when called from different threads. -- Scott Ribe scott_r...@killerbytes.com http://www.killerbytes.com/ (303) 722-0567 voice _

Re: Global in NSApplication

2010-01-27 Thread Graham Cox
On 28/01/2010, at 3:37 AM, Scott Ribe wrote: > Of course this code does not necessarily *prevent* you from making multiple > Debug instances, but neither does the code Graham posted if you try to call > [Debug sharedDebug] from multiple threads. Calling a singleton method from multiple threads

Re: Global in NSApplication

2010-01-27 Thread Scott Ribe
> It would be a bad > idea to make any AppKit calls before NSApplicationMain, for example. Not too hard to avoid that in the -init method ;-) > This also makes it difficult to use the Debug class in a framework/ > library. Yes, use from framework/library is the primary motivator for using a sing

Re: Global in NSApplication

2010-01-27 Thread Jens Alfke
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

Re: Global in NSApplication

2010-01-27 Thread Scott Ribe
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); } Then in any file where you want to use it, put "extern Debug *gDebug;"

Re: Global in NSApplication

2010-01-27 Thread BareFeet
On 27/01/2010, at 5:02 PM, Graham Cox wrote: > The singleton pattern Jens suggested is simpler. I can't think of any simpler > way: > > + (Debug*)sharedDebug > { >static Debug* s_debug = nil; > >if( s_debug == nil ) >s_debug = [[self alloc] init]; > >return s_de

Re: Global in NSApplication

2010-01-26 Thread Graham Cox
On 27/01/2010, at 4:43 PM, BareFeet wrote: > I just wanted to check if there's a simpler way. I guess not. The singleton pattern Jens suggested is simpler. I can't think of any simpler way: + (Debug*) sharedDebug { static Debug* s_debug = nil; if( s_debug == nil )

Re: Global in NSApplication

2010-01-26 Thread BareFeet
Yes, I'm familiar with categories, but didn't mention it because it's so similar to subclassing. I just wanted to check if there's a simpler way. I guess not. Thanks, Tom BareFeet Sent from my iPhone On 27/01/2010, at 1:17 PM, Murat Konar wrote: Read up on categories. Then make the instance

Re: Global in NSApplication

2010-01-26 Thread Jens Alfke
Even easier, use the Singleton design pattern and implement a +sharedInstance class method in your class. This is how existing singletons like NSFileManager work. --Jens {via iPhone} On Jan 26, 2010, at 6:17 PM, Murat Konar wrote: Read up on categories. Then make the instance of your De

Re: Global in NSApplication

2010-01-26 Thread Murat Konar
Read up on categories. Then make the instance of your Debug class a static variable in your category implementation file, and use a method you added to NSApplication via your category to return it. _murat On Jan 26, 2010, at 6:05 PM, Ba

Global in NSApplication

2010-01-26 Thread BareFeet
Hi all, I've created a "Debug" class in my app, which contains an instance variable and several methods. I want to add this to my application but can't see how. I can add it to MyDocument class like this: - (id)init { self = [super init]; if (self) { // other stuff debug