block是NSObject对象
我们将object-c代码转换成c++,看看block的结构。执行 $ xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc -fobjc-arc main.m
将如下代码生成c++文件。
int main(int argc, const char * argv[]) {
@autoreleasepool {
int a = 10;
void (^block)(void) = ^(void){
NSLog(@"%d",a);
};
block();
NSLog(@"%@", [block class]);
NSLog(@"%@", [[block class] superclass]);
NSLog(@"%@", [[[block class] superclass] superclass]);
NSLog(@"%@", [[[[block class] superclass] superclass] superclass]);
}
return 0;
}
关键部分如下:
int main(int argc, const char * argv[]) {
/* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool;
int a = 10;
void (*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, a));
((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block);
NSLog((NSString *)&__NSConstantStringImpl__var_folders_6p_k8pdm4dd3831fkms4mrm7_7w0000gn_T_main_2e3b50_mi_1, ((Class (*)(id, SEL))(void *)objc_msgSend)((id)block, sel_registerName("class")));
NSLog((NSString *)&__NSConstantStringImpl__var_folders_6p_k8pdm4dd3831fkms4mrm7_7w0000gn_T_main_2e3b50_mi_2, ((Class (*)(id, SEL))(void *)objc_msgSend)((id)((Class (*)(id, SEL))(void *)objc_msgSend)((id)block, sel_registerName("class")), sel_registerName("superclass")));
NSLog((NSString *)&__NSConstantStringImpl__var_folders_6p_k8pdm4dd3831fkms4mrm7_7w0000gn_T_main_2e3b50_mi_3, ((Class (*)(id, SEL))(void *)objc_msgSend)((id)((Class (*)(id, SEL))(void *)objc_msgSend)((id)((Class (*)(id, SEL))(void *)objc_msgSend)((id)block, sel_registerName("class")), sel_registerName("superclass")), sel_registerName("superclass")));
NSLog((NSString *)&__NSConstantStringImpl__var_folders_6p_k8pdm4dd3831fkms4mrm7_7w0000gn_T_main_2e3b50_mi_4, ((Class (*)(id, SEL))(void *)objc_msgSend)((id)((Class (*)(id, SEL))(void *)objc_msgSend)((id)((Class (*)(id, SEL))(void *)objc_msgSend)((id)((Class (*)(id, SEL))(void *)objc_msgSend)((id)block, sel_registerName("class")), sel_registerName("superclass")), sel_registerName("superclass")), sel_registerName("superclass")));
}
return 0;
}
先看block被调用的部分:((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block);
。它将block指针转成了__block_impl *
类型。
而在__main_block_impl_0
的构造方法中,如下:
struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
int a;
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int _a, int flags=0) : a(_a) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
可以看到impl.isa = &_NSConcreteStackBlock
这行代码中有isa指针,也就是说block指针指向的结构体有isa指针。我们再看看这部分代码:
NSLog(@"%@", [block class]);
NSLog(@"%@", [[block class] superclass]);
NSLog(@"%@", [[[block class] superclass] superclass]);
NSLog(@"%@", [[[[block class] superclass] superclass] superclass]);
它的打印结果如下:
2019-06-18 10:51:58.747875+0800 Block[71723:27889252] __NSMallocBlock__
2019-06-18 10:51:58.747891+0800 Block[71723:27889252] __NSMallocBlock
2019-06-18 10:51:58.747905+0800 Block[71723:27889252] NSBlock
2019-06-18 10:51:58.747917+0800 Block[71723:27889252] NSObject
可以看出,block是一个NSObject对象。它继承至NSBlock,而NSBlock继承至NSObject。
Block对变量的捕获
1、局部变量
对于局部变量,block捕获的是值,也就是值传递:
int main(int argc, const char * argv[]) {
/* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool;
int a = 10;
void (*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, a));
}
return 0;
}
2、静态变量
对于static修饰的变量,block捕获的是变量的地址,也就是地址传递。普通指针也是值传递:
int main(int argc, const char * argv[]) {
/* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool;
static int a = 10;
void (*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, &a));
}
return 0;
}
3、NSObject类型变量
对于NSObject类型变量,block是地址捕获,默认是__strong
类型:
struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
NSObject *__strong obj;//强引用
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, NSObject *__strong _obj, int flags=0) : obj(_obj) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
int main(int argc, const char * argv[]) {
/* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool;
NSObject *obj = ((NSObject *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("NSObject"), sel_registerName("new"));
void (*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, obj, 570425344));
}
return 0;
}
所以,这种情况下,外部始终强引用block,会使block捕获的对象内存泄漏。
4、__block
__block int a = 10;
转成c++,变成__attribute__((__blocks__(byref))) __Block_byref_a_0 a = {(void*)0,(__Block_byref_a_0 *)&a, 0, sizeof(__Block_byref_a_0), 10};
。可以看出__block
修饰的局部变量会封装成__Block_byref_a_0
类型的结构体。而__Block_byref_a_0
的结构如下:
struct __Block_byref_a_0 {
void *__isa;
__Block_byref_a_0 *__forwarding;
int __flags;
int __size;
int a;
};
block捕获的是参数a(__Block_byref_a_0
结构体)中__forwarding
参数的地址。
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是__Block_byref_a_0结构体的地址,这里捕获的是_a的__forwarding参数的地址
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
修改a的值的代码:a = 20
转成c++,变成(a->__forwarding->a) = 20;
。也就是说,修改 a的值变成修改__Block_byref_a_0
结构体中a变量的值。
5、__weak
用$ xcrun -sdk iphoneos clang -rewrite-objc -F UIKit -fobjc-arc -arch arm64 -stdlib=libc++ -mmacosx-version-min=10.7 -fobjc-runtime=macosx-10.7 -Wno-deprecated-declarations main.m
命令将下面代码转成c++。
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSObject *obj = [NSObject new];
__weak typeof(obj) weakObj = obj;
void (^block)(void) = ^(void){
__strong typeof(weakObj) strongObj = weakObj;
NSLog(@"%@",strongObj);
};
}
return 0;
}
关键代码如下:
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
NSObject *__weak weakObj = __cself->weakObj; // bound by copy
__attribute__((objc_ownership(strong))) typeof(weakObj) strongObj = weakObj;
NSLog((NSString *)&__NSConstantStringImpl__var_folders_6p_k8pdm4dd3831fkms4mrm7_7w0000gn_T_main_494bdc_mi_0,strongObj);
}
int main(int argc, const char * argv[]) {
/* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool;
NSObject *obj = ((NSObject *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("NSObject"), sel_registerName("new"));
__attribute__((objc_ownership(weak))) typeof(obj) weakObj = obj;
void (*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, weakObj, 570425344));
}
return 0;
}
从void (*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, weakObj, 570425344));
这一句可以看出block捕获的是weakObj。也就是一个弱引用。
而在__main_block_func_0(调用block执行的方法)中又将weakObj转换成了strongObj。
Block有三种类型
Block有三种类型,分别为__NSGlobalBlock__
、__NSMallocBlock__
和__NSStackBlock__
。它们都继承至NSBlock。__NSGlobalBlock__
存储在数据段, __NSMallocBlock__
存储在堆中,__NSStackBlock__
存储在栈中。