015-iOS底层原理-block

2021-09-01  本文已影响0人  Mr_wick

引言-“毒鸡汤”

一个好的iOS开发,block必不可少,都会用,但是block的底层原理,我们确都是“浅尝辄止”,满足开发就好。人们总说“俗话说”,但是很多俗话说,不都是前人一步一个坑结结实实的踩了,才总结出来的一句话。那么俗话说“逆水行舟,不进则退”,是否也可以用在开发上?我觉得可以。薪水可以变,但是时间不等人。技术深度(广度)应该是至少跑赢时间(可能大盘看多了,收益跑赢大盘这句话就常挂嘴边)。
希望每一位开发者,都应该具备“危机意识”,在有限的时间,学到更多的技术。
话不多说,今天这个文章,是探索block的底层原理。

本文Demo

block 底层结构

【1】普通block

按照我们探索类的内存时,用到的探索方式,首先查看编译成底层c++的代码是怎么样的。 在main.m中设计代码如下:

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        int a = 1001;
        NSString *str = @"百事可乐";
        void (^qlBlock)(void) = ^{
            NSLog(@"a = %d -- str = %@",a,str);
        };
        
        qlBlock();
    }
    return 0;
}

打开终端,通过xcrun指令将main.m转成main.cpp文件:

xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc main.m -o main.cpp

打开main.cpp,拉到最底下,找到main函数。将括号内的类型转换删除,得到精简代码如下:

int main(int argc, const char * argv[]) {
    /* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool; 
        int a = 1001;
        NSString *str = (NSString *)&__NSConstantStringImpl__var_folders_wv_3h_d7hqj7zz300r8bmzy6_y00000gn_T_main_2abb7f_mi_0;
        // __main_block_impl_0函数
        void (*qlBlock)(void) = __main_block_impl_0(__main_block_func_0, &__main_block_desc_0_DATA, a, str, 570425344);

        qlBlock->FuncPtr(qlBlock);
    }
    return 0;
}

1、void (*qlBlock)(void)赋值为一个函数调用__main_block_impl_0 ()
2、搜索__main_block_impl_0找到block的底层实际上也是一个结构体,并且__main_block_impl_0 ()block的构造函数,源码如下:

struct __main_block_impl_0 {
  struct __block_impl impl;
  struct __main_block_desc_0* Desc;
  int a; // 将block外部的变量a捕获成为自己的成员变量
  NSString *str; // 将block外部的变量str捕获成为自己的成员变量
  // 构造函数:
  // 参数1、void *fp:传的是函数__main_block_func_0(),该函数就是block的执行代码块 ^{ },这个fp传给了 FuncPtr,在qlBlock->FuncPtr(qlBlock);中调用
  // 参数2、struct __main_block_desc_0 *desc:block的信息结构体
  // 参数3、4 外部捕获的变量,a(_a), str(_str) 分别表示_a赋值给a,_str赋值给str
  // 
  __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int _a, NSString *_str, int flags=0) : a(_a), str(_str) {
    impl.isa = &_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
};

3、qlBlock->FuncPtr(qlBlock);调起block,FuncPtr已经在前面的构造函数中赋值为__main_block_func_0 ()函数。因此,此处调的就是这个函数,并把block自己传进去。该block已捕获了外部变量a,str

【2】外部变量加上 __block关键字

我们将外部变量a和str分别加上__block关键字,并在block执行块中修改a和str,main.m代码设计如下:

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        __block int a = 1001;
        __block NSString *str = @"百事可乐";
        void (^qlBlock)(void) = ^{
            a = 2002;
            str = @"可口可乐";
            NSLog(@"a = %d -- str = %@",a,str);
        };
        qlBlock();
    }
    return 0;
}

打开终端,通过xcrun指令将main.m转成main.cpp文件:

xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc main.m -o main.cpp

打开main.cpp,拉到最底下,找到main函数。将括号内的类型转换删除,得到精简代码如下:

1、block的结构体
struct __main_block_impl_0 {
  struct __block_impl impl;
  struct __main_block_desc_0* Desc;
  __Block_byref_a_0 *a; // by ref
  __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, __Block_byref_a_0 *_a, int flags=0) : a(_a->__forwarding) { // 将 _a-> __forwarding赋值给a
    impl.isa = &_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
};
2、main函数
int main(int argc, const char * argv[]) {
    /* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool; 
        __Block_byref_a_0 a = {0,&a, 0, sizeof(__Block_byref_a_0), 1001};
        // __main_block_impl_0()
        void (*qlBlock)(void) = (__main_block_impl_0(__main_block_func_0, &__main_block_desc_0_DATA, &a, 570425344));
        // 调用
        qlBlock->FuncPtr)(qlBlock);
    }
    return 0;
}
3、生成的a结构体
struct __Block_byref_a_0 {
    void *__isa;
    __Block_byref_a_0 *__forwarding;
    int __flags;
    int __size;
    int a;
};
4、block执行的代码块
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {// 传入block自己
    // 获取block生成的并赋值好的a
    __Block_byref_a_0 *a = __cself->a; // bound by ref
    // 结构体a通过__forwarding获取结构体a内部的成员变量a,并进行修改
    (a->__forwarding->a) = 2002;
    
    NSLog((NSString *)&__NSConstantStringImpl__var_folders_wv_3h_d7hqj7zz300r8bmzy6_y00000gn_T_main_6d01e1_mi_0,(a->__forwarding->a));
}

由编译后的源码可知,
1、外部变量加了__block后,block对外部变量的捕获,不再是单纯的生成与之对应的成员变量,而是在内部生成__Block_byref_a_0 *a的结构体指针变量。
2、在main函数中,先将外部的__block int a编译后,生成一个结构体__Block_byref_a_0(其内部如上3、生成的a结构体,该结构体内部生成一个对应的int a,用于接收外部int a的初始值1001,同时将外部int a的地址&a赋值给__forwarding),初始化赋值代码:__Block_byref_a_0 a = {0,&a, 0, sizeof(__Block_byref_a_0), 1001};
3、到此为止,生成的结构体a的__forwarding为外部int a的地址,
block结构体内部的*a与外部变量a所指向的内存空间是同一个,捕获的外部变量会随着block生成的对应成员变量改变而改变。

block 底层结构小结

1、block的本质是一个结构体;
2、block的定义,是通过block的结构体内部的构造函数,对block内部的impl、desc进行赋值。
3、结构体impl内部有4个成员变量:isa、Flags、Reserved、FuncPtr,在block构造函数赋值时,将isa赋值为默认的NSConcreteStackBlock地址(此为编译阶段,运行时这个isa会赋值成真实类型),FuncPtr 的赋值是定义的block执行代码块。
4、block捕获外部变量,是在block内部生成对应的成员变量,并通过构造函数将捕获的变量赋值给内部生成的成员变量--(值拷贝)
5、block捕获的外部变量,如果用__block修饰,则不再是将值传给block内部生成的变量,而是将外部变量的地址传给内部成员变量,达到你动我动的效果--(指针拷贝)

block 源码

1、在苹果官网Source Browser下载libclosure-79,解压打开Blocks工程。
2、在原来的objc工程main.m中,定一个最简单的block,并打上断点,运行到断点后,打开汇编(Debug--Debug Workflow -- Always Show Disassembly)。


3、在汇编中,查看block的类型,以及将要call的符号
4、点击step into,进入到objc_retainBlock函数中,可看到将要访问_Block_copy此函数即可将 stack block 复制成 malloc block
5、继续点击step into会回到main的汇编。因此_Block_copy是我们继续探索的函数符号。打开1的Blocks工程。全局搜索_Block_copy,源码如下(注意看注释)
// Copy, or bump refcount, of a block.  If really copying, call the copy helper if present.
void *_Block_copy(const void *arg) {
    // block的真实结构Block_layout
    struct Block_layout *aBlock;

    if (!arg) return NULL;
    
    // The following would be better done as a switch statement
    aBlock = (struct Block_layout *)arg;
    // 如果是释放状态,没必要进行下一步处理,直接返回aBlock
    if (aBlock->flags & BLOCK_NEEDS_FREE) {
        // latches on high
        latching_incr_int(&aBlock->flags);
        return aBlock;
    }
    else if (aBlock->flags & BLOCK_IS_GLOBAL) {
        // 如果是global类型的block,直接返回。
        return aBlock;
    }
    else {
        // 如果不是global block,那么只有两种情况:1、栈block(StackBlock),2、堆block(MallocBlock)
        // 在编译阶段,暂时会将block标记成栈block,是因为在编译阶段,若是对block进行malloc开辟内存的话,会增加编译器的压力
        // 来到运行时(runtime),block捕获了外部变量,则需要将栈block 的大小,malloc(size)开辟一个内存空间
        // Its a stack block.  Make a copy.
        size_t size = Block_size(aBlock);
        struct Block_layout *result = (struct Block_layout *)malloc(size);
        if (!result) return NULL;
        // 开始copy
        memmove(result, aBlock, size); // bitcopy first
#if __has_feature(ptrauth_calls)
        // Resign the invoke pointer as it uses address authentication.
        // invoke的copy
        result->invoke = aBlock->invoke;

#if __has_feature(ptrauth_signed_block_descriptors)
        if (aBlock->flags & BLOCK_SMALL_DESCRIPTOR) {
            uintptr_t oldDesc = ptrauth_blend_discriminator(
                    &aBlock->descriptor,
                    _Block_descriptor_ptrauth_discriminator);
            uintptr_t newDesc = ptrauth_blend_discriminator(
                    &result->descriptor,
                    _Block_descriptor_ptrauth_discriminator);

            result->descriptor =
                    ptrauth_auth_and_resign(aBlock->descriptor,
                                            ptrauth_key_asda, oldDesc,
                                            ptrauth_key_asda, newDesc);
        }
#endif
#endif
        // reset refcount
        // 重置refcount和配置flags
        result->flags &= ~(BLOCK_REFCOUNT_MASK|BLOCK_DEALLOCATING);    // XXX not needed
        result->flags |= BLOCK_NEEDS_FREE | 2;  // logical refcount 1
        _Block_call_copy_helper(result, aBlock);
        // Set isa last so memory analysis tools see a fully-initialized object.
        // 将block类型标记为堆block(MallocBlock)
        result->isa = _NSConcreteMallocBlock;
        return result;
    }
}

6、通过源码可知流程如下:
a)oc定义的block 不 捕获外部变量 ---->编译器--->global block---->运行时--->return global block
b)oc定义的block捕获外部变量 ---->编译器--->stack block ---->运行时_Block_copy--->malloc block
7、block的底层结构为struct Block_layout结构体,源码如下(注意注释):

struct Block_layout {
    // isa的指向:也可以说是block的类型:1、global block;2、stack block;3、malloc block
    void * __ptrauth_objc_isa_pointer isa;
    // 标识
    volatile int32_t flags; // contains ref count
    int32_t reserved;
    // 执行函数
    BlockInvokeFunction invoke;
    // block的信息描述
    struct Block_descriptor_1 *descriptor;
    // imported variables
};
#define BLOCK_DESCRIPTOR_1 1
struct Block_descriptor_1 {
    uintptr_t reserved;
    uintptr_t size;
};
#define BLOCK_DESCRIPTOR_2 1
struct Block_descriptor_2 {
    // requires BLOCK_HAS_COPY_DISPOSE
    BlockCopyFunction copy;
    BlockDisposeFunction dispose;
};
#define BLOCK_DESCRIPTOR_3 1
struct Block_descriptor_3 {
    // requires BLOCK_HAS_SIGNATURE
    const char *signature;
    const char *layout;     // contents depend on BLOCK_HAS_EXTENDED_LAYOUT
};
struct Block_descriptor_small {
    uint32_t size;
    int32_t signature;
    int32_t layout;
    /* copy & dispose are optional, only access them if
       Block_layout->flags & BLOCK_HAS_COPY_DIPOSE */
    int32_t copy;
    int32_t dispose;
};
// getter 函数
static inline void *_Block_get_descriptor(struct Block_layout *aBlock)
{
    void *descriptor;
#if __has_feature(ptrauth_signed_block_descriptors)
    if (!(aBlock->flags & BLOCK_SMALL_DESCRIPTOR)) {
        descriptor =
                (void *)ptrauth_strip(aBlock->descriptor, ptrauth_key_asda);
    } else {
        uintptr_t disc = ptrauth_blend_discriminator(
                &aBlock->descriptor, _Block_descriptor_ptrauth_discriminator);
        descriptor = (void *)ptrauth_auth_data(
                aBlock->descriptor, ptrauth_key_asda, disc);
    }
#elif __has_feature(ptrauth_calls)
    descriptor = (void *)ptrauth_strip(aBlock->descriptor, ptrauth_key_asda);
#else
    descriptor = (void *)aBlock->descriptor;
#endif
    return descriptor;
}
// setter函数
static inline void _Block_set_descriptor(struct Block_layout *aBlock, void *desc)
{
    aBlock->descriptor = (struct Block_descriptor_1 *)desc;
}
static struct Block_descriptor_2 * _Block_descriptor_2(struct Block_layout *aBlock)
{
    uint8_t *desc = (uint8_t *)_Block_get_descriptor(aBlock);
    desc += sizeof(struct Block_descriptor_1);// 看下图解释
    return (struct Block_descriptor_2 *)desc;
}
static struct Block_descriptor_3 * _Block_descriptor_3(struct Block_layout *aBlock)
{
    uint8_t *desc = (uint8_t *)_Block_get_descriptor(aBlock);
    desc += sizeof(struct Block_descriptor_1);
    if (aBlock->flags & BLOCK_HAS_COPY_DISPOSE) {
        desc += sizeof(struct Block_descriptor_2);
    }
    return (struct Block_descriptor_3 *)desc;
}

block 调试

1、未捕获外部变量:回到objc工程,在main.m添加如下代码,并添加断点:


2、捕获外部变量:
3、添加_Block_copy符号断点,运行时,先暂时关闭_Block_copy断点,因为在运行时会有一些系统的block会调用此函数进行复制,我们只需要在自定义的block断点停住时,把_Block_copy断点激活即可。打开Debug 汇编

3.1:我们通过lldb调试的指令register read读取寄存器的情况(真机的寄存器名字为x0,x1等),最终看到在_Block_copy函数内部的汇编block的类型的变化,对应上面的_Block_copy源码即可。
3.2:继续点击step over,一直回到main.m时,在block调用处打上断点。通过lldb调试的指令register read读取寄存器的情况,如图所示:
若外部变量为对象,则会多一个copy和dispose。即7.1中的block信息描述选择。
<__NSStackBlock__: 0x7ffeefbff428>
 signature: "v8@?0"
 invoke   : 0x1000038e0 (/Users/monan/Library/Developer/Xcode/DerivedData/LGProject-dyfiqisfsswhupfyzablyalixxxt/Build/Products/Debug/QLObjcTest`__main_block_invoke)
 copy     : 0x100003910 (/Users/monan/Library/Developer/Xcode/DerivedData/LGProject-dyfiqisfsswhupfyzablyalixxxt/Build/Products/Debug/QLObjcTest`__copy_helper_block_e8_32s)
 dispose  : 0x100003950 (/Users/monan/Library/Developer/Xcode/DerivedData/LGProject-dyfiqisfsswhupfyzablyalixxxt/Build/Products/Debug/QLObjcTest`__destroy_helper_block_e8_32s)

3.3:signature的值可参考前面的文章类的方法底层,是一样的。具体例子:lldb调试:[NSMethodSignature signatureWithObjCTypes:"v8@?0"];打印结果如下(注意注释):

(lldb) po [NSMethodSignature signatureWithObjCTypes:"v8@?0"];
<NSMethodSignature: 0x7afa2cf5db0b894b>
    number of arguments = 1
    frame size = 224
    is special struct return? NO
    return value: -------- -------- -------- --------
        type encoding (v) 'v'
        flags {}
        modifiers {}
        frame {offset = 0, offset adjust = 0, size = 0, size adjust = 0}
        memory {offset = 0, size = 0}
    argument 0: -------- -------- -------- --------
        type encoding (@) '@?' // 证明了block的encoding类型是`@?`
        flags {isObject, isBlock}
        modifiers {}
        frame {offset = 0, offset adjust = 0, size = 8, size adjust = 0}
        memory {offset = 0, size = 8}

block 捕获

【1】无__block修饰

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSObject *obj = [NSObject alloc];
        void (^ qlBlock)(void) = ^{
            NSLog(@"---%@---",obj);
        };
        qlBlock();
    }
    return 0;
}
xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc main.m -o main.cpp

将代码进行排版,清除一些类型强转,简化后的代码如下:

 __main_block_desc_0_DATA = { 0, sizeof(struct __main_block_impl_0), __main_block_copy_0, __main_block_dispose_0};

具体对应的变量为:

size_t reserved = 0;
size_t Block_size = sizeof(struct __main_block_impl_0);
void (copy)(struct __main_block_impl_0, struct __main_block_impl_0) = __main_block_copy_0;
void (
dispose)(struct __main_block_impl_0*) = __main_block_dispose_0;

【2】有__block修饰

我们在NSObject *obj前加入__block,然后 按照第【1】步的步骤,将main.m编译成c/c++。
将代码进行排版,清除一些类型强转,简化后的代码如下:

【3】_Block_object_assign

static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {
    _Block_object_assign(&dst->obj, src->obj, 8/*BLOCK_FIELD_IS_BYREF*/);
}

static void __main_block_dispose_0(struct __main_block_impl_0*src) {
    _Block_object_dispose(src->obj, 8/*BLOCK_FIELD_IS_BYREF*/);
}

_Block_object_assign()传入了三个参数:1、&dst->obj:目标block的obj地址;2、src->obj:原block的obj变量;3、8:BLOCK_FIELD_IS_BYREF,表示该对象用__block 修饰了。

The flags parameter of _Block_object_assign and _Block_object_dispose is set to
* BLOCK_FIELD_IS_OBJECT (3), for the case of an Objective-C Object,
* BLOCK_FIELD_IS_BLOCK (7), for the case of another Block, and
* BLOCK_FIELD_IS_BYREF (8), for the case of a __block variable.

即:

  • 捕获的外部变量为普通oc对象,则传入3;
  • 捕获的外部变量为其他block,则传入7;
  • 捕获的外部变量为用__block修饰的变量,则传入8;
void _Block_object_assign(void *destArg, const void *object, const int flags) {
    const void **dest = (const void **)destArg;
    switch (os_assumes(flags & BLOCK_ALL_COPY_DISPOSE_FLAGS)) {
      case BLOCK_FIELD_IS_OBJECT:
    
        /*******
        id object = ...;
        [^{ object; } copy];
        ********/
        _Block_retain_object(object);
        *dest = object;
        break;

      case BLOCK_FIELD_IS_BLOCK:
        /*******
        void (^object)(void) = ...;
        [^{ object; } copy];
        ********/

        *dest = _Block_copy(object);
        break;
    
      case BLOCK_FIELD_IS_BYREF | BLOCK_FIELD_IS_WEAK:
      case BLOCK_FIELD_IS_BYREF:
        /*******
         // copy the onstack __block container to the heap
         // Note this __weak is old GC-weak/MRC-unretained.
         // ARC-style __weak is handled by the copy helper directly.
         __block ... x;
         __weak __block ... x;
         [^{ x; } copy];
         ********/

        *dest = _Block_byref_copy(object);
        break;
        
      case BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT:
      case BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK:
        /*******
         // copy the actual field held in the __block container
         // Note this is MRC unretained __block only. 
         // ARC retained __block is handled by the copy helper directly.
         __block id object;
         __block void (^object)(void);
         [^{ object; } copy];
         ********/

        *dest = object;
        break;

      case BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT | BLOCK_FIELD_IS_WEAK:
      case BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK  | BLOCK_FIELD_IS_WEAK:
        /*******
         // copy the actual field held in the __block container
         // Note this __weak is old GC-weak/MRC-unretained.
         // ARC-style __weak is handled by the copy helper directly.
         __weak __block id object;
         __weak __block void (^object)(void);
         [^{ object; } copy];
         ********/

        *dest = object;
        break;

      default:
        break;
    }
}

分析:
通过flags & BLOCK_ALL_COPY_DISPOSE_FLAGS得到捕获的变量是什么类型,通过switch分支进行分类处理。

【4】_Block_byref_copy

全局搜索_Block_byref_copy,得到源码如下:

static struct Block_byref *_Block_byref_copy(const void *arg) {
    struct Block_byref *src = (struct Block_byref *)arg;

    if ((src->forwarding->flags & BLOCK_REFCOUNT_MASK) == 0) {
        // src points to stack
        struct Block_byref *copy = (struct Block_byref *)malloc(src->size);
        copy->isa = NULL;
        // byref value 4 is logical refcount of 2: one for caller, one for stack
        copy->flags = src->flags | BLOCK_BYREF_NEEDS_FREE | 4;
        copy->forwarding = copy; // patch heap copy to point to itself
        src->forwarding = copy;  // patch stack to point to heap copy
        copy->size = src->size;

        if (src->flags & BLOCK_BYREF_HAS_COPY_DISPOSE) {
            // Trust copy helper to copy everything of interest
            // If more than one field shows up in a byref block this is wrong XXX
            struct Block_byref_2 *src2 = (struct Block_byref_2 *)(src+1);
            struct Block_byref_2 *copy2 = (struct Block_byref_2 *)(copy+1);
            copy2->byref_keep = src2->byref_keep;
            copy2->byref_destroy = src2->byref_destroy;

            if (src->flags & BLOCK_BYREF_LAYOUT_EXTENDED) {
                struct Block_byref_3 *src3 = (struct Block_byref_3 *)(src2+1);
                struct Block_byref_3 *copy3 = (struct Block_byref_3*)(copy2+1);
                copy3->layout = src3->layout;
            }

            (*src2->byref_keep)(copy, src);
        }
        else {
            // Bitwise copy.
            // This copy includes Block_byref_3, if any.
            memmove(copy+1, src+1, src->size - sizeof(*src));
        }
    }
    // already copied to heap
    else if ((src->forwarding->flags & BLOCK_BYREF_NEEDS_FREE) == BLOCK_BYREF_NEEDS_FREE) {
        latching_incr_int(&src->forwarding->flags);
    }
    return src->forwarding;
}

【5】byref_keep

由上图我们可知byref_keep()是为了保存(保活)外部捕获变量的生命周期,如果不进行keep,则有可能会出现src为空了之后,开辟的空间的内容也被清空了。
byref_keep的源码为:

struct Block_byref_2 {
    // requires BLOCK_BYREF_HAS_COPY_DISPOSE
    BlockByrefKeepFunction byref_keep;
    BlockByrefDestroyFunction byref_destroy;
};

返回到main_byref.cpp文件中,找到struct __Block_byref_obj_0{}结构体,内部的copydispose函数分别赋值给了byref_keepbyref_destroy。也就是说在编译后的cpp文件中,在main函数里,__block修饰的NSObject *obj,编译所生成的结构体的赋值,__Block_byref_id_object_copy_131赋值给了结构体__Block_byref_obj_0copy函数;__Block_byref_id_object_dispose_131赋值给了__Block_byref_obj_0dispose函数。
也就是说:

byref_keep = __Block_byref_id_object_copy_131函数
byref_destroy = __Block_byref_id_object_dispose_131函数

由此可知,byref_keep函数的调用,是调用__Block_byref_obj_0copy函数。也就是把_Block_object_assign调用一遍,即

// (*src2->byref_keep)(copy, src);
static void __Block_byref_id_object_copy_131(void *dst, void *src) {
    _Block_object_assign(dst + 40, * (src + 40), 131);
}
struct __Block_byref_obj_0 {
    void *__isa; // 8
    __Block_byref_obj_0 *__forwarding;// 8
    int __flags;        // 4
    int __size;         // 4
    void (*__Block_byref_id_object_copy)(void*, void*);// 8
    void (*__Block_byref_id_object_dispose)(void*);// 8
    NSObject *obj;
};

_Block_object_assign(dst + 40, * (src + 40), 131);变成为_Block_object_assign(obj, * obj, 131);。由上面_Block_object_assign函数的源码可知,此刻的switch分支,走的就是BLOCK_FIELD_IS_OBJECT分支。
因此,block捕获__block修饰的外部变量,

总结

上一篇下一篇

猜你喜欢

热点阅读