This is the 11th day of my participation in the August More Text Challenge

1. Block type

Blocks are the most commonly used chunk of code, and they come up a lot in interviews. Blocks are objects that encapsulate a piece of code that can be executed at any time. A block can be a function argument or a function return value, and can itself take input arguments or return values. Like the proxy protocol, it can also be used for event communication. Blocks fall into three categories

1.1 Global Block

A global block, located in a global area, that uses no external variables inside the block, or just static variables and global variables.

Do not use external variables:Use internal variables:Using static variables:

1.2 MallocBlock

Block, located in the heap, uses local variables or oc attributes inside the Block and assigns values to strongly referenced or copy-modified variables.

Using local variables:Use attributes:

1.3 StackBlock

Stack Block, located on the stack, is similar to heap blocks in that blocks use local variables or OC attributes inside, but cannot be assigned to strongly referenced or copy-modified variables.

1.4 Understanding of Block

We have a distinction between blocks. Global blocks are easier to understand, but heap blocks and stack blocks are easier to confuse. Let’s take a look:

Run crash, we implement our own custom Block and set its callback to nil.

  1. __weakThe modified block uses local variables inside and is thereforeThe stack area blcokThe block itself is an object and therefore copies the pointer.
  2. After strongBlock – > strongBlock1

althoughstrongBlock1Make a copy of itThe heap areaBut it’s a callbackinvokealreadynil.

weweakBlock copyAfter that, it is equivalent to deep copy, which does not affect the previous weakBlock operation.

  • Impact of reference counting

1. StrongBlock internal access is not a static variable, and there is no weak reference modifier is a heap Block, so objC holdshold+ 1,copy+ 1.

2. WeakBlock is a weak reference modification and therefore isThe stack area block, justholdobjc +1.

3. The mallocBlockThe stack area blockCopy is thereforeHeap area block, but weakBlock has handled the external reference object, mallocBlock no longer handles objC, just copies the stack block to the heap, objC +1.

We put themallocBlock = [strongBlock copy]Block does not increase the reference count because they are all in the heap and just copy Pointersobject.

Moving on to the interview question: Circular references?

Circular reference:staticSelf_ -> weakSelf -> self->staticSelf_

2. Block circular reference

self->block->self, causing the loop to hold, unable to release resulting in memory leaks. Usually the system will tell you.

Circular references are not created without circular holding.

  • Normal releaseWhen A calls the dealloc method and sends A release signal to B, B receives the release signal. If B’s retainCount (reference count) is 0, B’s dealloc method will be called

  • A circular reference: A and B hold each other, so A cannot call dealloc method to send release signal to B, and B cannot receive release signal either. So neither A nor B can be released

To solve the problem of circular references, we need to break the closed loop in the following ways.

2.1 weak – strong – dance

  • We often use__weakModifier does not change self’s reference count.

  • To paraphrase block

Normally there is no problem after 2 seconds, but if we return before 2 seconds, the execution of self has been destroyed and cannot execute properly.This is where we need to strongly reference self

Among themstrongSelfIs a temporary variable within the scope of testBlock, that is, insideBlock performedThe release of thestrongSelf

2.2 __block modifies variables

through__blockModifies objects, mainly because __block modifies objects that can be changed, manually set to nil using completion.But you need to call this block, otherwise ifDon't call block,Vc will not be null, so it will still be a circular reference, and self and block will not be freed

2.3 Hold objects as parameters

Take the object self asparameterFor internal use within blocks, there are no reference counting problems.

3. Summary

Block is mainly divided into three types: global block, heap block and stack block. Block is not only a code block, but also an object, essentially a structure. Can be arguments, return values, properties. The use of blocks makes our logic more compact, more convenient to pass values. Note that cyclic references may cause memory leaks. Weak-strong-dance (weak and strong dance), __block modifier variable, manually empty; Hold objects as parameters, etc. The next article will explore the principles of blocks.