简体   繁体   中英

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];
    }];

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM