Block is just like other objective c object, it can be retained if a variable strong refer to it.
When block is used as method argument, the block will retain or keep a strong reference to all the object variables used within the block. Usually, after the block is executed, the block will be released and then the block will also release all the variables it refers to within the block implementation.
This may cause retain cycle, if any variable referred by the block also retain the block itself. As quite ofter, the block is used as a completion handler and saved as a property for later use. The result is, the main (self) object keeps a reference to the block as a property, and the block keeps a reference to the main (or self) object automatically when the main or self object, or any of its property or instance member is used within the block. So even if all external references have been set to nil for the main object, the object by itself can keep alive as long as the property that refers to block is not set to nil.
One of the solution is using __block declaration. But it may cause other troubles. As when __block is used, block will not automatically retain the object to keep it alive, so if the object is no longer be referred in any other place, it will be release and as a result, when block is executed, the object may become nil. If the application does not handle the case, it may cause crash at runtime. Similarly, create a weak reference variable just before creating the block and then use the weak variable within the block works in the same way.
Note, when __block is not used, it only prevents the object itself to be changed within block, but data of the object, such as its properties or instance member can still be change as usual.
When block is used as method argument, the block will retain or keep a strong reference to all the object variables used within the block. Usually, after the block is executed, the block will be released and then the block will also release all the variables it refers to within the block implementation.
This may cause retain cycle, if any variable referred by the block also retain the block itself. As quite ofter, the block is used as a completion handler and saved as a property for later use. The result is, the main (self) object keeps a reference to the block as a property, and the block keeps a reference to the main (or self) object automatically when the main or self object, or any of its property or instance member is used within the block. So even if all external references have been set to nil for the main object, the object by itself can keep alive as long as the property that refers to block is not set to nil.
One of the solution is using __block declaration. But it may cause other troubles. As when __block is used, block will not automatically retain the object to keep it alive, so if the object is no longer be referred in any other place, it will be release and as a result, when block is executed, the object may become nil. If the application does not handle the case, it may cause crash at runtime. Similarly, create a weak reference variable just before creating the block and then use the weak variable within the block works in the same way.
Note, when __block is not used, it only prevents the object itself to be changed within block, but data of the object, such as its properties or instance member can still be change as usual.