Amogh Talpallikar
Amogh Talpallikar

Reputation: 12184

When do we create memory for ivars in Singleton example by Apple?

I have seen the Apple's example of Singleton and couple of other examples. People say that it is too strict!

But the point is ..even if it is too strict, I want to understand it. I dont understand that when we call allocWithZone on super, What happens ?

Memory will be created according to super's instance size.

What if our Singleton has ivars ?

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid/TP40002974-CH4-SW32

I also dont understand that, Why does allocWithZone return the object with a retain call when retain itself is returning the object as it is.

Upvotes: 0

Views: 113

Answers (1)

CRD
CRD

Reputation: 53000

Whenever a method is called in Obj-C it is passed an object reference via the hidden parameter self. For an instance method self refers to the object the method was invoked on, for a class method self refers to the the class object (of type Class) the method was invoked on. Calls to super implicitly pass on self.

Therefore in Apple's example code the call [super allocWithZone:NULL] calls the super implementation of allocWithZone passing the current value of self, which is MyGizmoClass's class object as it is a static method. The implementation of allocWithZone can determine the required memory size from the passed Class object – the details of how are private.

As you've correctly spotted, the call to retain in allocWithZone is pointless but harmless.

Upvotes: 1

Related Questions