1

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?

ShurupuS
  • 2,923
  • 2
  • 24
  • 44
  • 6
    Sounds to me like you're trying to make a singleton class? Singleton classes are alive for the entire duration your application is running so you never deallocate it. – rocky Apr 09 '13 at 16:45
  • @rocky Thanks, I read a little about it - so is it normal or not? I',m the new one in iOS development - sorry if my questions are too stupid) – ShurupuS Apr 09 '13 at 16:53
  • Is what normal or not? – rocky Apr 09 '13 at 16:54
  • 1
    Assuming your goal here is to have a singleton, you need to do some searching on the proper way to create a singleton. Your code is definitely setup incorrectly. Example, see http://stackoverflow.com/questions/5720029/create-singleton-using-gcds-dispatch-once-in-objective-c – rmaddy Apr 09 '13 at 17:00
  • 1
    This isn't a correct singleton pattern. If this is all the code, it shouldn't even work. See http://stackoverflow.com/questions/5720029/create-singleton-using-gcds-dispatch-once-in-objective-c – Rob Napier Apr 09 '13 at 17:13

1 Answers1

4

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!

Reid
  • 1,109
  • 1
  • 12
  • 27
  • Thank u, this is a test project for my future job - so I'm truing to understand the iOS development from the zero point. And to make the app compatible with the old iOS versions I have to use memory management.. Now I have a great number of leaks.. And I try to understand how can I resolve this situation( – ShurupuS Apr 09 '13 at 17:28
  • I can understand that, and I like knowing the nuts-and-bolts myself. Still, realize that it is relatively easy to convert old code to using ARC. In the XCode menu, "Editor-Refactor-Convert to ARC" will get you most of the way there! I firmly believe that you have nothing to gain by learning the old way--ARC handles it better than even the best programmers would manually. Just my opinion, though. Up to you. – Reid Apr 09 '13 at 21:15
  • 1
    @ShurupuS You can use ARC and still support back to iOS 4.3. – rmaddy Apr 09 '13 at 22:24