One type of memory leak

I installed singleton following the instructions in this tutorial , but when I analyze it, I see the following memory leaks:

enter image description here

How to fix this memory leak in my single class?

+3
source share
4 answers

I think that the one who wrote this tutorial did not write it correctly:

 [[self alloc] init];

Instead, it could be:

_sharedMySingleton = [[MySingleton alloc]init];

I hope this helps

+6
source

You do not assign selection to a variable. Change it like this:

+(MySingleton*)sharedMySingleton
{
    @synchronized(self)
    {
      if (!_sharedMySingleton)
           _sharedMySingleton = [[self alloc] init];
    }

    return _sharedMySingleton;
}

EDIT my input was too slow, others already answered :)

+5
source

It looks like when you return _sharedMySingleton, it will still be zero. And, therefore, allocated the next time. You should try to set it when the selection is complete.

0
source

[[self alloc] init]; - not assigned to an object

0
source

Source: https://habr.com/ru/post/1788468/


All Articles