iOS block的值捕获与指针捕获详解

来自:网络
时间:2022-02-08
阅读:
目录

指针与指针变量

通俗的理解:

指针:内存地址
指针变量:存放内存地址的变量
指针变量的指针:指针变量自身的内存地址

Person *p = [Person new]

iOS block的值捕获与指针捕获详解

右边isa为:对象的内存地址 - 指针

p为:指针变量

左边isa为:指针变量的内存地址 - 指针变量的指针

block捕获变量方式

对局部变量捕获有两种形式:1、值捕获(局部自动变量) 2、指针捕获(局部静态变量);全局变量无需捕获,可直接进行访问。

clang -rewrite-objc **.m -o **.cpp 不同场景下转换成C++代码结果如下(嫌代码长不想看的直接看代码下面的结论)

值捕获

指针变量的捕获

block内部用一个新的指针变量来接收原指针变量。接收后,两个指针变量里面存储的值都是对象的内存地址,所以也可以说是值的捕获。

局部自动变量:

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Person *p = [Person new];
        void (^block)(void) = ^{
            NSLog(@"%@",p);
        };
        block();
    }
    return 0;
}
struct Person_IMPL {
    struct NSObject_IMPL NSObject_IVARS;
};

struct __main_block_impl_0 {
  struct __block_impl impl;
  struct __main_block_desc_0* Desc;
  Person *p;
  __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, Person *_p, int flags=0) : p(_p) {
    impl.isa = &_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
};

static void __main_block_func_0(struct __main_block_impl_0 *__cself) {

  Person *p = __cself->p; // bound by copy
            NSLog((NSString *)&__NSConstantStringImpl__var_folders_7w_wgxxl_655s9g6tms_7z44s6w0000gn_T_main_f76e59_mi_0,p);
}

static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {_Block_object_assign((void*)&dst->p, (void*)src->p, 3/*BLOCK_FIELD_IS_OBJECT*/);}

static void __main_block_dispose_0(struct __main_block_impl_0*src) {_Block_object_dispose((void*)src->p, 3/*BLOCK_FIELD_IS_OBJECT*/);}

static struct __main_block_desc_0 {
  size_t reserved;
  size_t Block_size;
  void (*copy)(struct __main_block_impl_0*, struct __main_block_impl_0*);
  void (*dispose)(struct __main_block_impl_0*);
} 
__main_block_desc_0_DATA = { 0, sizeof(struct __main_block_impl_0), __main_block_copy_0, __main_block_dispose_0};

int main(int argc, const char * argv[]) {
    /* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool;
        Person *p = ((Person *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("Person"), sel_registerName("new"));
        
        void (*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, p, 570425344));
        
        ((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block);

    }
    return 0;
}

代码分析,生成的__main_block_impl_0结构体里面创建了一个指针变量p,main函数里面的__main_block_impl_0初始化时,传入的也是指针变量p。所以block对局部自动变量采用的捕获方式是指针变量的捕获,也就是值捕获。

指针捕获

对指针变量自身指针的捕获

block内部用一个新的指针来接收(指向)原指针变量自身的地址。

局部静态变量:

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        static Person *p = nil;
        p = [Person new];
        void (^block)(void) = ^{
            NSLog(@"%@",p);
        };
        block();
    }
    return 0;
}
struct Person_IMPL {
    struct NSObject_IMPL NSObject_IVARS;
};

struct __main_block_impl_0 {

  struct __block_impl impl;
  struct __main_block_desc_0* Desc;
  Person **p;
  
  __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, Person **_p, int flags=0) : p(_p) {
    impl.isa = &_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
};

static void __main_block_func_0(struct __main_block_impl_0 *__cself) {

  Person **p = __cself->p; // bound by copy
  NSLog((NSString *)&__NSConstantStringImpl__var_folders_7w_wgxxl_655s9g6tms_7z44s6w0000gn_T_main_bd39c2_mi_0,(*p));
}

static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {_Block_object_assign((void*)&dst->p, (void*)src->p, 3/*BLOCK_FIELD_IS_OBJECT*/);}

static void __main_block_dispose_0(struct __main_block_impl_0*src) {_Block_object_dispose((void*)src->p, 3/*BLOCK_FIELD_IS_OBJECT*/);}

static struct __main_block_desc_0 {

  size_t reserved;
  size_t Block_size;
  void (*copy)(struct __main_block_impl_0*, struct __main_block_impl_0*);

  void (*dispose)(struct __main_block_impl_0*);

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

int main(int argc, const char * argv[]) {

    /* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool;

        static Person *p = __null;
        p = ((Person *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("Person"), sel_registerName("new"));

        void (*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, &p, 570425344));
        ((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block);
    }
    return 0;
}

代码分析,生成的__main_block_impl_0结构体里面创建了一个指针*p,main函数里面的__main_block_impl_0初始化时,传入的是指针变量p的地址&p。所以block对局部静态变量采用的捕获方式是指针变量自身地址的捕获,也就是指针捕获。

__block修饰的变量

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        __block Person *p = [Person new];
        void (^block)(void) = ^{
            NSLog(@"%@",p);
        };
        block();
    }
    return 0;
}
struct Person_IMPL {
    struct NSObject_IMPL NSObject_IVARS;
};

struct __Block_byref_p_0 {
  void *__isa;
__Block_byref_p_0 *__forwarding;
 int __flags;
 int __size;
 void (*__Block_byref_id_object_copy)(void*, void*);
 void (*__Block_byref_id_object_dispose)(void*);
 Person *p;
};

struct __main_block_impl_0 {
  struct __block_impl impl;
  struct __main_block_desc_0* Desc;
  
  __Block_byref_p_0 *p; // by ref
  
  __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, __Block_byref_p_0 *_p, int flags=0) : p(_p->__forwarding) {
    impl.isa = &_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
};

static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
  __Block_byref_p_0 *p = __cself->p; // bound by ref
  NSLog((NSString *)&__NSConstantStringImpl__var_folders_7w_wgxxl_655s9g6tms_7z44s6w0000gn_T_main_6c171f_mi_0,(p->__forwarding->p));
}

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

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

static struct __main_block_desc_0 {
  size_t reserved;
  size_t Block_size;
  void (*copy)(struct __main_block_impl_0*, struct __main_block_impl_0*);
  void (*dispose)(struct __main_block_impl_0*);
} __main_block_desc_0_DATA = { 0, sizeof(struct __main_block_impl_0), __main_block_copy_0, __main_block_dispose_0};

int main(int argc, const char * argv[]) {

    /* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool;
        __attribute__((__blocks__(byref))) __Block_byref_p_0 p = {(void*)0,(__Block_byref_p_0 *)&p, 33554432, sizeof(__Block_byref_p_0), __Block_byref_id_object_copy_131, __Block_byref_id_object_dispose_131, ((Person *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("Person"), sel_registerName("new"))};

        void (*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, (__Block_byref_p_0 *)&p, 570425344));

        ((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block);

    }
    return 0;
}

代码分析,使用__block修饰的指针变量p,会被转换为__Block_byref_p_0的结构体,结构体内持有p。main函数里面初始化__main_block_impl_0时传入的是__Block_byref_p_0的地址,访问p时,通过__Block_byref_p_0的__forwarding指针进行访问。其实就相当于block内部捕获了__Block_byref_p_0的指针,通过指针去访问__Block_byref_p_0持有的p。所以__block修饰的变量本质上也相当于是一种指针捕获,只不过不是直接捕获指针变量p的自身地址。

值捕获能否重新赋值? 进行值拷贝时,block内部同名指针变量如果执行重新赋值操作,相当于使内部的指针变量指向了一个新的对象,再对此对象进行任何操作都与原指针变量指向的原对象无关,所以不能进行重新赋值。

指针捕获能否重新赋值? block内部将block外部的指针变量的指针赋值给一个新的指针,block内部、外部的指针都指向的是同一个指针变量。如果进行赋值操作,操作的是同一个指针变量,所以可以进行重新赋值。

关于block延伸的知识点

如果文章看到了这里,相信对值捕获和指针捕获已经有了一个清晰的认识,那么可以自行思考以下几个问题,看是否真的理解了block,文章没有的答案放在评论区

  • 值捕获能否在block内被重新赋值?如果是静态变量呢?(文中已有)
  • 经__block修饰变量生成的持有变量的结构体里面__forwarding的意义在于什么?
  • 使用block有什么需要注意的点,如何去解决?
返回顶部
顶部