简体   繁体   中英

Can two weak variables create a retain cycle?

In ARC, if A hold a strong reference to B, and B holds a strong ref to A, there will be a retain cycle.

Will the code below also create a retain cycle?

    __weak MyClass *weakSelf = self;
    [self doSomething:^{

        weakSelf.someVariable = YES;

        [weakSelf doSomething:^{

            weakSelf.someVariable = YES;
        }];

    }];

With ARC the weakSelf pointer is copied, but since it's weak, the copy of the pointer will not cause the retain count to be increased. So no, it doesn't create a retain cycle.

No it won't. In fact, if you dont retain the object and for some reason the block would be called after the MyClass object gets deallocated, you end up with a runtime crash

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