Bogdan Balta
Bogdan Balta

Reputation: 121

Does a singleton create retain cycle inside block?

I have a singleton and I want to capture it inside a block. I know that variables are retained inside a block because a constant copy of the objects passed is created and never deallocated unless using a weak instance of that object. The curiosity I have is whether the same behaviour is applied to a statically allocated variable. Here is my code (self is the sharedInstance of Class):

    + (Class *)sharedInstance
    {
        static Class *sharedInstance = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            sharedInstance = [[Class alloc] init];
        });
        return sharedInstance;
    }

    [self setBlock:^(NSArray *array)
    {
        self.property = [array firstObject];
    }];

Upvotes: 3

Views: 2332

Answers (2)

Andrew Paul Simmons
Andrew Paul Simmons

Reputation: 4513

Yes, the block will retain the singleton forever! But the singleton is always going to be in memory. So, there is nothing to be released. Hence, there is absolutely no reason to use weakSelf if the self object is a singleton. A singleton is an object that lives in memory forever.

Upvotes: 3

rob mayoff
rob mayoff

Reputation: 385590

Your claim that “a constant copy of the objects passed is created” is incorrect. The block creates __strong or __weak or __unsafe_unretained references to the objects that it uses; it doesn't copy the objects.

Your code creates a retain cycle between self and the block object, because the block has a strong reference to self and (assuming self.property is strong) self has a strong reference to the block.

The compiler doesn't understand the concept of a singleton. It doesn't know there's anything special about the lifetime of sharedInstance. So it doesn't do anything different with the block just because you've set up self as a singleton.

Upvotes: 2

Related Questions