Block Is Used for Communication between OC and C++
Background
In iOS development, we often encounter the situation of mixing OC and C++, so how to communicate between then? This article will introduce how to use block for communication between oc and c++. Due to the different memory management style of oc and c++, you may encounter problems when using block. This article will answer them one by one.
Objective-C uses OC as its abbreviation in this article.
2. Knowledge points that must be mastered before reading
2.1 OC memory management
OC uses counting references to manage memory and is RAC. Simply put, when pointing to an instance, the reference count is +1, when it is released, it is -1, and when it is 0, it is released.
2.2 Block
- Essence: The essence of block in oc is an object
- Storage: global area, heap area
- Creation: Usually the created block is stored in the global area, and variables on the stack or heap are captured to the heap area.
2.3 C++ memory management
- Manual release, pointer release uses release (pointer), object release uses delete object
2.4 Stack memory release
- The data in the stack space does not need to be released
- The memory in the heap space needs to be released. oc is automatically released by its RAC mechanism, and c++ is released manually by the developer.
3. Problem Description
3.1 Problem description
The c++ object points to a block. The block captures the oc variable and copies it to the heap area. However, the block in the heap area will be automatically released when the function body ends. At a certain point, the c++ object calls the block and will crash because the block Already released.
3.2 Code
1 | @interface TestA () |
4.How to solve
4.1 Steps
(1) Create an oc object A, which holds a block.
(2) Pass the block of object A to the c++ object. There is an attribute in the c++ object pointing to the block.
(3) The C++ object calls block and triggers the callback function in object A.
4.2 Code implementation
1 | OC file |
5. Summary
5.1 Principle analysis
The block is created in the global area and will not be released, but once the instance is captured, it will be copied to the heap.
However, the memory in the heap space will be automatically released when the count reference is 0. After the block is initialized, the count reference is 0 and the function body is released, so calling block in C++ will crash.
5.2 Solution description
Therefore, we need to extend the life cycle of the block and hold the block through the object. Under the premise that the object is not released, the block value exists, and there will be no problem of crash caused by c++ calls.