ajay
ajay

Reputation: 3345

handling the singleton object

Sorry to ask basic and some what more important tricky question.I have a singleton class which is already existed in my application( unfortunately i didn't implemented that class).Now i am checking the whole code of the project using the analyzer singleton class showing me the memory leak.I am not sure the code is correct or not because handle singleton class is very tricky.Please any one suggest me why analyzer giving me leak.I attached the singleton class methods please check in the screenshot singleton

In my project we are calling [className sharedDataSource]; many times.Please provide me some knowledge on this Thanks in advance.

Upvotes: 0

Views: 163

Answers (2)

govi
govi

Reputation: 686

I think its quite safe to ignore that warning really.. For more explanation of how the singleton really behaves as a singleton, you may find this link pretty illuminating.

Long story short, all calls to alloc, in turn call allocwithzone, and in this allocwithzone, there is a condition that makes sure that the alloc happens only once. Agreed, the implementation is quite wonky. However there are quite a few different ways to implement a singleton, as evidenced here, if you feel like refactoring it later or may be even just for academic interest.

Upvotes: 1

Bill Bonar
Bill Bonar

Reputation: 1027

I think this is correct. Think about it in terms of what a singleton does. A singleton is a class that lives and there is only a single instance for the entire application. It is a purposeful memory leak. That class is declared and is not supposed be released so that it is available later.

If you use this pattern in objective-c, I think you are responsible for clearing the memory when the singleton is no longer needed.

Upvotes: 1

Related Questions