Summary of Common Circular References in IOS

  • 2021-12-09 10:16:30
  • OfStack

Summary of Common Circular References in IOS

Introduction:

Circular reference refers to multiple objects referencing each other, so that the reference forms a ring, resulting in the external unable to really drop this ring memory. Actually, it's a bit like deadlock.

For example: A- > B- > C- > ....- > X- > B - > Represents a strong reference, The reference count of such B is 2. If A is released by the system, theoretically A will automatically reduce the resource referenced by A, that is, B, then the reference count of B will become 1, and all B cannot be released, but A has been released, and the memory part of all B will definitely not be released and reused, resulting in memory leakage.

Situation 1: delegate

Delegate is the most commonly encountered circular reference in the development of ios. 1 Generally, when declaring delegate, we should use weak reference weak or assign


@property (nonatomic, weak, nullable) id <UITableViewDelegate> delegate;

Of course, how to choose whether to use assign or weak, assign can only be used if MRC, and weak is best used when ARC is used, because the variable modified by weak automatically points to nil after whether or not, so as to prevent the existence of unsafe wild pointers

Situation 2: Block

Block is also a common problem of circular reference. When self is used in Block, it is easy to have circular reference. Therefore, when many people use block, they will declare a __weak to modify self. In fact, this is not the case. Not all people who use Block will have Self circular reference problem. Only self has a strong reference to Block will have this situation.

Therefore, the temporary use of Block in a function will not lead to circular application, because the Block reference belongs to the stack at this time. When the block on the stack is released, the reference count of self in block is also subtracted

Of course, Self must have a direct reference to Block before it appears. If there is an Block variable in self and B, this situation is easy to occur. The good thing is that if there is a circular reference in block, xcode7 will appear a warning prompt (the previous version is uncertain).

Situation 3: NSTimer

This is a magical NSTimer, when you create the use of NSTimer, NSTimer will default to the current self has a strong reference, all in the self use to complete whether or not, 1 must first use NSTimer invalidate to stop whether the time control of the reference to self


[_timer invalidate];

Summary:

What we said above is common. In fact, circular reference means that our strong reference forms a closed loop, and there will be a lot of code written by ourselves, so we should pay attention to writing at ordinary times. Of course, xcode's instruments can also help you eliminate some such similar memory problems.

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: