ShurupuS
ShurupuS

Reputation: 2923

Where to release the shared instance in iOS

I have a shared instance (a simple data controller) and in my project I don't use ARC.

static ECOMDataController *sharedInstanse;
@implementation ECOMDataController
+(ECOMDataController *)sharedInstance
{
    return sharedInstanse;
}
-(id)init
{
    [self checkAndCreateDataFileIfExist];
    [self readAppFile];
    if (sharedInstanse)
        NSLog(@"The shared instance was created already.");
    sharedInstanse = self;
    return self;
}

And I use it in the other methods like this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    dataController = [ECOMDataController sharedInstance];
    [dataController readAppFile];
    [[self tableView] reloadData];
}

As I can see from the leaks instrument - I have a memory leak here - what should I do to release the data controller? And where is better to do that?

Upvotes: 1

Views: 1153

Answers (1)

Reid
Reid

Reputation: 1129

Rocky is right: you wouldn't deallocate a singleton. Frankly, I wouldn't use that pattern at all--except for system calls like AppDelegate or NSNotificationCenter. There are a lot of pitfalls with the pattern...but that's my opinion (though I'm not alone in it).

More importantly, why are you not using ARC? There's absolutely no reason not to, and many reasons for it. Especially for a newer developer, there's no sense in fussing about memory management when the compiler will do it for you, anyway--and will do a better job of it. You have enough to learn without fussing over retain counts!

Upvotes: 4

Related Questions