4

Can Someone explain me few things regarding the Singleton implementation in Apple's documentation here.

Link: - http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html

Go to Creating a Singleton Instance in the link.

I tried but couldn't understand few things:-

  • What does [super allocWithZone:NULL] in + (MyGizmoClass*)sharedManager does.?
  • Why does allocWithZone call sharedManger method and return a retain call on its return type when retain itself returns self ?
  • If the Singleton has some instance variables in it, where should they be initialized ?

If anyone could briefly explain working of allocWithZone and sharedManager methods here , a lot of these questions would automatically be answered.

Amogh Talpallikar
  • 12,084
  • 13
  • 79
  • 135

2 Answers2

2

That implementation is generally considered over_kill. There is a lot of protections against a programmer trying to mis-use the singleton which is generally not considered necessary.

Here is an example of a more simple implementation from Yoga:

+ (id)sharedFoo
{
    static dispatch_once_t once;
    static MyFoo *sharedFoo;
    dispatch_once(&once, ^ { sharedFoo = [[self alloc] init]; });
    return sharedFoo;
}
Community
  • 1
  • 1
zaph
  • 111,848
  • 21
  • 189
  • 228
  • Explained in [Care and Feeding of Singletons](http://www.mikeash.com/pyblog/friday-qa-2009-10-02-care-and-feeding-of-singletons.html) and [Singletons: You're doing them wrong](http://cocoasamurai.blogspot.com/2011/04/singletons-your-doing-them-wrong.html). Also recommended by Apple at [dispatch_once](http://developer.apple.com/library/mac/documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html#//apple_ref/c/func/dispatch_once) – Jano Jan 09 '12 at 14:42
  • Note that this is not sufficient to build a true singleton, in that more than one instance can still be created. It's necessary to override `allocWithZone:` to actually prohibit other instantiations. See also [Peter Hosey's blog post](http://boredzo.org/blog/archives/2009-06-17/doing-it-wrong). – jscs Jan 09 '12 at 19:06
  • Why is it marked as an answer? Does it answer what asker has asked? No offense to Zaph's effort but it sounded to me misleading. – rohan-patel Jan 05 '13 at 20:23
2

Here goes - I have paraphrased your questions:

What does [super allocWithZone:NULL] do?

This is the same as saying [super alloc]. The withZone part is to do with where in memory your object will be stored. In practice, it would be very rare to use it. See this question for more info - what is difference between alloc and allocWithZone:?

Why does the retain method return itself (and not increment the retain counter)

Singletons are kept alive throughout the life of your application - you don't care about the retain count, because there's no situation in which you would want to deallocate your singleton. retain returns self as a courtesy and convention (and to allow nested expressions).

If the Singleton has some instance variables in it, where should they be initialized ?

Up to you. Typically you would initialise them in the init method, as per a normal object.

Community
  • 1
  • 1
lxt
  • 31,146
  • 5
  • 78
  • 83
  • But what does call to alloc of super does ? regarding retain, I asked something else. retain is returning self but why does allocwithzone call [sharedmanager retain] ? why doesnt it also return self only ? – Amogh Talpallikar Jan 09 '12 at 14:49
  • 1
    You always call `[super alloc]` when you are a sub-class of something, because your parent class needs to allocate its variables. As has been alluded to in other answers, Apple's example is not really the best starting point here. – lxt Jan 09 '12 at 15:00
  • Wow, thanks for pointing that out. My brain is a bit screwy today - I meant the `init` method. – lxt Jan 09 '12 at 23:22