Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of block Loop reference problem

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you the example analysis of the block circular citation problem, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Starting from iOS4.0, block technology has been introduced into objective C. Block is similar to the function of standard c, and the introduction of block makes code callbacks more convenient.

Advantages:

(1) the callback is written directly where it needs to be triggered to make the code more continuous.

(2) Local variables can be accessed within the code block

However, if block is not used carelessly, it will cause a memory leak.

1. Why does it cause memory leaks?

In the block code block, if an object outside the code block is passed in, block will retain the object and hold the object, resulting in a circular reference.

Eg:

@ interface TestBlockObject: NSObject

-(void) invokeBlock: (void (^) (void)) testBlock

@ end

@ implementation TestBlockObject

-(void) invokeBlock: (void (^) (void)) testBlock {

NSLog (@ "TestBlockObject")

}

@ end

/ / call

@ interface ViewController ()

{

TestBlockObject * ob

}

@ end

@ implementation ViewController

-(void) viewDidLoad {

[super viewDidLoad]

/ / Do any additional setup after loading the view, typically from a nib.

Ob = [TestBlockObject new]

[ob invokeBlock: ^ {

/ / cause circular reference

[self testPrint]

}]

}

-(void) didReceiveMemoryWarning {

[super didReceiveMemoryWarning]

/ / Dispose of any resources that can be recreated.

}

-(void) testPrint {

NSLog (@ "")

}

@ end

The place that causes the circular reference

[ob invokeBlock: ^ {

/ / cause circular reference

[self testPrint]

}]

ViewController (self) holds self in the block that holds ob,ob, resulting in a circular reference. The self object will not be destroyed, so the ob will not be destroyed, causing a memory leak.

2. The method of solving circular reference

Replace the red code with the following code

_ _ weak typeof (self) weakSelf = self

[ob invokeBlock: ^ {

If (weakSelf) {

[weakSelf testPrint]

}

}]

Break circular references by converting self into weak objects and then using them in block.

3. Special places

If you add the following code to the viewDidLoad of ViewController, it will not cause a circular reference. Because the code block is a class method, ViewController does not hold that part of the memory. When the block execution finishes, the block is released, so the self is not held.

[UIView animateWithDuration:0.5 animations: ^ {

[self testPrint]

}]

The above is all the content of the article "sample Analysis of block Loop citation problems". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report