Actually the -init method sets the static _sharedMyClass variable, so this is fine. See: stackoverflow. Com/questions/145154/… – pix0r May 6 '09 at 16:24 2 Then I would guess that CLANG isn't smart enough to infer that the self alloc return is actually setting the self parameter, which will be saved by the init method.It thus thinks it's just lost (as I did, without more context).
– Adam Wright May 6 '09 at 16:26 LOL, you are correct. Thanks. – pix0r May 6 '09 at 18:10.
Apple has since updated their recommended singleton code to pass the static analyzer: + (MyGizmoClass*)sharedManager { if (sharedGizmoManager == nil) { sharedGizmoManager = super allocWithZone:NULL init; } return sharedGizmoManager; } + (id)allocWithZone:(NSZone *)zone { return self sharedManager retain; } Now +sharedManager calls super's -allocWithZone: and assigns the return of -init, and the singleton's -allocWithZone: just returns a retained sharedInstance. Edit: Why the retain in +allocWithZone:? +allocWithZone: is overridden because someone using MyGizmoClass could circumvent the singleton by calling MyGizmoClass alloc init instead of MyGizmoClass sharedManager.It's retained because +alloc is expected to always return an object with a retain count of +1.
Every call to +alloc should be balanced with a -release or -autorelease, so without the retain in +allocWithZone:, the shared instance could potentially be deallocated out from under other users.
I like this approach but I don't understand the need for the retain in allocWithZone. Can somebody explain it to me? – n8gray Dec 14 '10 at 22:41 Answered with an edit to my original answer.
– Justin Anderson Dec 16 '10 at 3:41.
You may be interested in a simple, one-method, GCD-based singleton implementation (and thus 10.6+ only) posted on Mike Ash's site: + (id)sharedFoo { static dispatch_once_t pred; static Foo *foo = nil; dispatch_once(&pred, ^{ foo = self alloc init; }); return foo; }.
Thanks, looks nice.. Unfortunately I'm actually on the iPhone platform so that's not an option yet. – pix0r Nov 13 '09 at 23:24 GCD is available in iOS 4, so have at it. Dispatch_once is definitely the way to go for singletons.
– Bill Garrison Mar 14 at 0:34.
You also probably had this in there too... + (id)allocWithZone:(NSZone *)zone { @synchronized(self) { if (sharedInstance == nil) { sharedInstance = super allocWithZone:zone; return sharedInstance; // assignment and return on first allocation } } return nil; // on subsequent allocation attempts return nil } The reason you weren't storing it in init is because you were storing it in the method that alloc called. This is the pattern Apple has in their examples. If you save the value in your init as well, all is fine and the warning goes away.
I'd leave the allocWithZone implementation alone.
You are referencing self in a class method! Secondly, you are calling self alloc init and just throwing away the instance. You should assign the singleton reference in the class method, and not in init like I am guessing you are doing.
Next, there is no real guarantee that _sharedMyClass will be initialized to zero. You should explicitly initialize it to nil.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.