简体   繁体   中英

Why do blocks create a retain cycle when referencing self?

I have read many articles that talk about blocks creating a retain cycle. But why would it make a retain cycle? Wouldn't the block release the reference after it got run?

[UIView animateWithDuration:5
                 animations:^{
            self.variable = somevalue;
}];

For example if the above were executed, the reference to self would be released right?

I already see that the above block creates two strong references. One from the object to the block and the block to the object. Can someone explain to me why the cycle doesn't break? Shouldn't the cycle break after the code is run?

If an object holds a block and the block holds the object then that's a cycle.

If someone else holds the block and the block holds the object then there's a cycle only if the object directly or indirectly holds whomever holds the block.

Blocks do not self destruct upon being run so the cycle will be broken only if the block is deallocated.

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