Take the 2-minute tour ×

I have a singleton class which is allocation object only one time in whole project. Few code line is below..

declaration... ....

MyClassName *classObject = nil

In the init method, the code is like below..

-(id) init(){
    self = [super init];

    if(classObject == nil){

        1. Allocate object code here
        2. classObject = self;
        3. return classObject
     } else {
        return classObject;

But my problem is that how will I dealloc this object. I am calling this init method from all the class and it is returning classObject reference every time.

My approach....

From the dealloc method in appdelegate.m, I am calling a function (releaseObject)which is defined in MyClassName . Here is the definition of function body...

 -(void) releaseObject {

    [self release]; // Confusion: Will it dealloc classObject reference?
    [super release];


Is this good idea dealloc this object? My problem is that I don't have to dealloc object until application is not being closed.

share|improve this question

4 Answers 4

up vote 1 down vote accepted

I'd recommend to make an extra class method in your singleton:

static MyClassName *classObject = nil;
+ (MyClassName *)sharedInstance {

+ (void)releaseSharedInstance {
  [classObject release];
  classObject = nil;
share|improve this answer

The answer is don't bother ever to release the singleton. It is supposed to last the lifetime of the application anyway and will go away automatically along with everything else when the process terminates. You may find that the dealloc method on your app delegate never gets called for the same reason.

If you have clean up to do on application close, it's probably better to move that into a separate method and just call that when the application is about to terminate.

a simpler pattern for a singleton IMO is to have a class method to return the singleton instance and not mess about with releasing stuff in init.

+(MyClass*) sharedInstance
    static MyClass* theInstance = nil;
    static dispatch_once_t pred;
    dispatch_once(&pred, ^{ theInstance = [[MyClass alloc] init]});
    return theInstance;

The above uses dispatch_once to ensure the initialising block only happens once in the lifetime of the application. You can still create other instances by calling init directly but that is advantageous IMO.

share|improve this answer
Would it be a cause of memory leakage, because I am allocating object in every class but in run time it will know whether it should allocate object or return previous stored reference. –  Ajay_Kumar May 21 '12 at 8:29
@Ajay_Kumar: technically it's a leak, in practice it isn't because the singleton will be reclaimed along with everything else when the process terminates. You will need to release self in the else part of your init method though. –  JeremyP May 21 '12 at 8:35

If your class is a singleton, in AppDelegete dealloc just call

[[YourClass instance] release];
share|improve this answer
As the structure its has concept of singletone but it is not exactly calling by the classname and instance. When we stop the app in simulator, does it not call dealloc in apdelegate.mm? –  Ajay_Kumar May 21 '12 at 8:21
When application terminates - operating system totally releases allocated memory. –  Moonkid May 21 '12 at 8:37

Here is workaround via Objective C++:

class StaticOwner {
    id<NSObject> object;
    StaticOwner(id<NSObject> obj) { object = [obj retain];  }
    ~StaticOwner() { [object release]; }
    id<NSObject> instance() {return object;}

Usage example:

+ (MySingleton*) sharedInstance {
    static StaticOwner owner = StaticOwner([[[MySingleton alloc] init] autorelease]);
    return owner.instance();
share|improve this answer

Your Answer


