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

IOS debugging methods tutorial on Block reference objects that cannot be released

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains the "iOS debugging Block reference object can not be released method tutorial", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and study the "iOS debugging Block reference object can not be released method tutorial"!

Block technology is very popular and convenient in iOS development, but a slight oversight can lead to the problem that references cannot be released, resulting in memory leaks. So how do you know which Block holds the object and causes a memory leak?

One solution is to use Xcode's Debug Memory Graph to view all objects in the lifecycle of the current process while the program is running. This allows you to use this feature to find objects that should have been released but have not been released during debugging. To determine which objects are suspected of memory leaks.

When you click on an object, you can see the memory allocation of the object and how it is referenced on the right, so you can further track and confirm who holds and references the object without being released normally.

The black line in the image above is the sequence diagram in which the object is strongly referenced.

Going back to the theme, you can see in the above graph that the object ViewController2 is held by a * * _ _ NSMallocBlock__**, but you can only see the memory address of the Block object (upper right corner). To see the implementation code for this Block, you only need to enter the following information in the lldb console:

(lldb) dis-s * (void**) (0x600002f51110+16) MyLoadTest` _ 27-[ViewController2 loadView] _ block_invoke: 0x10c79c080: pushq% rbp 0x10c79c081: movq% rsp,% rbp 0x10c79c084: subq $0x40,% rsp 0x10c79c088: movq% rdi,-0x8 (% rbp) 0x10c79c08c: movq% rdi,% rax 0x10c79c08f: movq $0x0,-0x10 (% rbp) 0x10c79c097: leaq-0x10 (% rbp),% rcx 0x10c79c09b: movq% rdi,-0x20 (% rbp)

The function of the dis-s address in the above instruction is to disassemble the symbol information corresponding to an address and the assembly implementation of the first part.

The 0x600002f51110 after the command is the address of the Block object, which means that 16 is added because the internal offset of the Block object is 16 bytes, which is the address of the function that executes the code saved by the Block object. So it's easy to know which Block object strongly holds the object without being released.

From the source code in the first figure above, you can see that the self object is held inside the Block so that the object cannot be released normally.

The above command can be used anywhere during debugging to view the function information of a Block.

It is important to note here that when you define multiple Block within a method. The rules for these Block functional symbols are:

-[method name of block definition] _ block_invoke. Serial number

The first block defined in the method has no ordinal number, while the subsequent one increments from 2 to 2 based on the number of definitions.

For example, the four block defined in the following class:

@ interface CA- (void) foo1 {void (^ b) (void) = ^ {}; void (^ b) (void) = ^ {};}-(void) foo2 {void (^ b) (void) = ^ {}; void (^ b) (void) = ^ {};} @ end

The symbol of the corresponding block is:

-[CA foo1] _ block_invoke- [CA foo1] _ block_invoke.2- [CA foo2] _ block_invoke- [CA foo2] _ block_invoke.2

Thank you for reading, the above is the "iOS debugging Block reference object can not be released method tutorial" content, after the study of this article, I believe you on iOS debugging Block reference object can not be released method tutorial this problem has a deeper understanding, the specific use of the need for you to practice verification. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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