Block
Circular references
The circular reference of block is relatively easy to be neglected, and it is also comparatively difficult to check out the problem. Of course, Apple has already done a circular reference check at the level of the Xcode compilation, so the examination of this problem suddenly becomes no more difficult.
Simply say the principle of circular referencing:The owner of the block references itself within the block scope, so that the owner of the block will never be able to free up memory, and there is a circular reference to the memory leak. Here's an example to illustrate:
@interface objtest () { void (^block) (); @end @implement objtest-(void) function { = ^() { ; };} @end
In this example, Objtest has a block object named block, and in this block, a member variable testvalue of Objtest is assigned. Thus, a circular reference was generated: Objtest->block->objtest.
The key to avoiding circular references is to break the closed loop. With only the ARC environment in mind, there is only one way I know to break this ring: use weak references to the owner within the block.
@interface objtest () { void (^block) (); @end @implement objtest-(void) function { __weak objtest* weakself = Self ; = ^() { ; };} @end
IOS Memory Management