0

I have a singleton object that is a module of my application and all objects created within this module has no relationship to any other module.My application creates this singleton object and this object creates further its child object that do not have any kind of relationship to other object in my application except this module.So after this module task is over i am setting this module object to nil.But two objects are not deallocating and showing in instruments of xcode.The singelton object is properly deallocating but its two child objects are remaining in memory.

My project is fully ARC. So what is the reason compiler is not deallocating singleton child object after it's parent(singelton object) set to nil.

Thanks in advance

codester
  • 36,891
  • 10
  • 74
  • 72

2 Answers2

1

But two objects are not deallocating and showing in instruments of xcode.The singelton object is properly deallocating but its two child objects are remaining in memory.

I fear that there is a retain cycle.
So for example child1 points to child2 and viceversa.To break that cycle you should use a weak reference.
I can't see the code so this is just an idea.

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
-1

Singleton is often a problem if you initialise more objects inside of it, which then call the super-object again. Because the singleton static variable has not yet been assigned.

You can prevent this by doing something like this:


Singleton file

+ (SomeClass *)sharedInstance {
    if (!_someStaticInstance) {
        _someStaticInstance = [[SomeClass alloc]init];
    }

    return _someStaticInstance;
}

- (void)initialise {
    // do the real initialising here
}

File creating it

[[SomeClass sharedInstance] initialise];
IluTov
  • 6,807
  • 6
  • 41
  • 103
  • can you explain little bit ? – codester Dec 22 '12 at 20:28
  • Do you see what I mean? If you do the initialising in the `init` method and call `[SomeClass sharedInstance]`, the singleton will call another `init` for you, which will give you a loop, or stackoverflow-exception. – IluTov Dec 22 '12 at 20:32
  • @SahilWasan Ok, then you'll have to post some code, so I can see what's wrong – IluTov Dec 22 '12 at 20:34
  • No i am not getting any exception and i am not intialising in init – codester Dec 22 '12 at 20:35
  • Thanks for your response but sorry i can not send my code because of some legal issues. – codester Dec 22 '12 at 20:39
  • This code is bad. See [this question](http://stackoverflow.com/q/5720029/730701) or [this answer](http://stackoverflow.com/a/145395/730701) to learn about singletons in Objective-C. – Adam Dec 23 '12 at 00:21
  • @Adam How for gods sake is this bad code?? Many roads lead to Rome – IluTov Dec 23 '12 at 00:31
  • @NSAddict First, you use init without alloc. This is more than bad. Second, if you use it from two or more separate threads at the same time, you can still end up with multiple instances. Just use GCD to create singleton-like shared instances. – Adam Dec 23 '12 at 00:38
  • @Adam The first thing was actually a typo, it wouldn't even have compiled. Thanks for the pointer. I guess I'll have to surrender because of the second argument. – IluTov Dec 23 '12 at 00:42