views:

65

answers:

1

I'm sure it is wishful thinking, but is there a counterpart to +initialize that will automatically get called when all instances of a class have been dealloc'ed?

I allocate a singleton object in +initialize, which is called before my first class instance gets alloc'ed. I would love to be able to release the object once my class instances have all been deallocated. However, if my objects of my class ever get reallocated, then I would need +initialize to get called again. I suspect Cocoa doesn't do that level of class management and it is probably up to me to wrap my class in a class manager class...

Am I correct?

A: 

There isn't.

I recommend just not removing a singleton, there's almost no point because it's just one object, which isn't going to take a lot of space.

If you really need this behavior, just override +alloc and +dealloc to count the current instances. (or +finalize in a garbage collected environment).

Georg
“Anyway, +initialize gets called when the class is loaded into memory.” You're thinking of `load`. `initialize` gets called when a class (or a subclass of it) receives its first message. See http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/ and http://www.friday.com/bbum/2009/09/06/iniailize-can-be-executed-multiple-times-load-not-so-much/
Peter Hosey
@Peter: Oh thanks, you're right.
Georg