Type of block

  • There are three types of blocks, which you can see by calling the class method or isa pointer. They are all derived from the NSBlock type

    • __NSGlobalBlock__ (_NSConcreteGlobalBlock)
    • __NSStackBlock__ (_NSConcreteStackBlock)
    • __NSMallocBlock__ (_NSConcreteMallocBlock)
  • These three types are stored in different areas of memory

    • __NSGlobalBlock__Data area in memory (.data area)
    • __NSStackBlock__Exists in the stack area of memory
    • __NSMallocBlock__The heap area of memory

What is the difference between the three types of blocks?

Two. Three types of blocks

  • First, set the program toMRCMode (ARCAt the back)

1.__NSGlobalBlock__

  • No internal useautoType variableblock, it is__NSGlobalBlock__type

2,__NSStackBlock__

  • Internally usedautoType variableblock, it is__NSStackBlock__type

3,__NSMallocBlock__

  • __NSStackBlock__The type ofblockcallcopyAfter is__NSMallocBlock__Type, bycopyThat will beblockCopy from stack to heap

  • __NSGlobalBlock__The type ofblockcallcopyThe post type is the same, or__NSGlobalBlock__Type (still in the data area)

  • __NSMallocBlock__The type ofblockcallcopyThe post type is the same, or__NSMallocBlock__Type (no new block generated, old reference count +1)

4. Summary of block types

3. Type of block in ARC environment

  • Set the program back to the ARC environment

  • In an ARC environment, the compiler automatically copies blocks on the stack to the heap as needed, such as the following

1.__NSStackBlock__Block of type is returned as a function return valueblockCopy to the heap

2,__NSStackBlock__Block of type assigned to__strongPointer, will beblockCopy to the heap

  • If there is no__strongPointer reference__NSStackBlock__The type ofblock, thenblockIs still of type__NSStackBlock__

3. Block is in the heap when it is used as a method parameter in Cocoa API

  • inCocoa APIWill have some methods, such as array traversal methods

4. When a block is a method parameter of the GCD API, the block is in the heap

Blocks in all four ways are copied to the heap

5,__NSGlobalBlock__Block of type, the type doesn’t change no matter what, still in the data area, okay

The auto variable of the object type

  • Create a classPersonAnd add oneageProperty, overridedeallocmethods

1. In MRC case, the auto variable of the object type

  • Set the program to MRC

  • We know that a local object variable is released when it leaves scope

  • If the stack areablockIs the auto object released when it is out of scope?

  • It’s in the stackblockIf the auto object is referenced, it will also be released when it leaves scope
  • If the stack areablockWhat happens when you copy it to the heap?

  • Based on the results, we know that if theblockCopy to the heap, at this pointpersonObject is not releasedblockThat’s true when it’s copied to the heappersonHave a go atretainTo deal with
  • If we release the heapblockAs you can seepersonAnd was released

  • Note that when a block is released from the heap, the AOTU object variable referenced in it is evaluated oncereleaseTo deal with

Conclusion: Blocks in the stack do not retain the auto variable of the object type, only when the block is copied to the heap (reference count +1) when the block in the heap is released, The auto variable of the object type is released (reference count -1), and if the auto variable of the object type has a reference count of zero, it is released

In ARC case, the auto variable of the object type

  • Set the program back to the ARC environment

  • In ARC, local object variables are also released when they leave scope

  • I’m going to use the auto variable in the block, and you can see thatpersonLeaving scope is not released

  • Only when theblockWhen out of scope is released,personTo be released

  • This is mainly because under ARC, one__NSStackBlock__The type ofblockBy a__strongWhen a pointer to theblockAutomatically copy to the heap
  • At this timeblockType is__NSMallocBlock__Will,personMake a strong reference (similar to that under MRCretainOperation)

3. View the block underlying structure

  • Using a terminal,cdtomain.mFile folder, and run the command
Xcrun-sdk iphoneOS clang-arch arm64-rewrite-fobjc-arc-fobjc-Runtime =ios-8.0.0 main.mCopy the code
  • Drag the generated main.cpp file into the project to open it

  • As you can see, the personauto variable is captured in __main_block_IMPL_0 and is of type __strong

  • And, in__main_block_desc_0There are also two more function Pointers incopyanddispose

  • The two function Pointers are passed as arguments__main_block_copy_0and__main_block_dispose_0Addresses of two functions

  • In fact, whenblockTo be copied toThe heapIs called__main_block_copy_0Function to captureThe auto variable of object typeStrong reference
  • whenblockIs called again when it is removed from the heap__main_block_dispose_0Function to captureThe auto variable of object typeRemoving strong references
  • On the stackblockEven if capturedThe auto variable of object typeAnd will not be called__main_block_copy_0Functions and__main_block_dispose_0Delta function, that is, it doesn’t pairThe auto variable of object typeStrong reference

4, __weak

  • Under ARC, the program provides__weakKeyword, used to modifyThe auto variable of object type
  • whenblockTo capture theThe auto variable of object typebe__weakWhen modifying, even ifblockIs copied to the heap,__main_block_copy_0Methods will not be caught eitherThe auto variable of object typeStrong reference

  • At this timeblockThe underlying structure is as follows:

  • At this time__main_block_desc_0There is stillcopyanddisposeJust not rightpersonStrong reference

Conclusion: In both ARC and MRC, the block in the stack does not strongly reference the captured object type auto (reference count +1). Instead, the block in the stack will strongly reference the object type Auto (ARC, __weak) when it is copied to the heap. Strong references are not made when a block is copied to the heap