对于block,在日常开发中经常会用到,但有时候会出现一些奇奇怪怪的问题,比如block的里面值没更新上,又比如block里面要改变一个外部变量的值,就得加上__block,不然编译器就会报错。
那么,这次就来一次过搞清楚block背后不为人知的真相。
Block的基本结构
void blockTest()
{
void (^block)(void) = ^{
NSLog(@"Hello World!");
};
block();
}
int main(int argc, char * argv[]) {
@autoreleasepool {
blockTest();
}
}
通过Clang在终端输入clang -rewrite-objc main.m,会生成一个cpp文件,打开可得:
struct __blockTest_block_impl_0 {
struct __block_impl impl;
struct __blockTest_block_desc_0* Desc;
__blockTest_block_impl_0(void *fp, struct __blockTest_block_desc_0 *desc, int flags=0) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
static void __blockTest_block_func_0(struct __blockTest_block_impl_0 *__cself) {
NSLog((NSString *)&__NSConstantStringImpl__var_folders_04_xwbq8q6n0p1dmhhd6y51_vbc0000gp_T_main_0048d2_mi_0);
}
static struct __blockTest_block_desc_0 {
size_t reserved;
size_t Block_size;
} __blockTest_block_desc_0_DATA = { 0, sizeof(struct __blockTest_block_impl_0)};
void blockTest()
{
void (*block)(void) = ((void (*)())&__blockTest_block_impl_0((void *)__blockTest_block_func_0, &__blockTest_block_desc_0_DATA));
((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block);
}
int main(int argc, char * argv[]) {
/* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool;
blockTest();
}
}
static struct IMAGE_INFO { unsigned version; unsigned flag; } _OBJC_IMAGE_INFO = { 0, 2 };
其实block的背后会生成一个结构体,这个结构体就是__blockTest_block_impl_0。
__blockTest_block_impl_0struct __blockTest_block_impl_0 {
struct __block_impl impl;
struct __blockTest_block_desc_0* Desc;
__blockTest_block_impl_0(void *fp, struct __blockTest_block_desc_0 *desc, int flags=0) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
__blockTest_block_impl_0是Block的C++实现,是一个结构体。包含了:
impl、__blockTest_block_desc_0* Desc和一个构造函数。
struct __block_impl {
void *isa;
int Flags;
int Reserved;
void *FuncPtr;
};
这里面有一个isa指针,指向一个类对象,有三种类型:_NSConcreteStackBlock、_NSConcreteGlobalBlock、_NSConcreteMallocBlock。Flags:block 的负载信息(引用计数和类型信息),按位存储。*FuncPtr:一个指针,指向Block执行时调用的函数,也就是Block需要执行的代码块。在本例中是__blockTest_block_func_0函数。
__blockTest_block_desc_0
static struct __blockTest_block_desc_0 {
size_t reserved;
size_t Block_size;
} __blockTest_block_desc_0_DATA = { 0, sizeof(struct __blockTest_block_impl_0)};
__blockTest_block_desc_0包含了两个变量:
reserved:Block版本升级所需的预留区空间,在这里为0。Block_size:Block大小(sizeof(struct __blockTest_block_impl_0))。可见,block也是会占用一定的内存大小的。
__blockTest_block_func_0static void __blockTest_block_func_0(struct __blockTest_block_impl_0 *__cself) {
NSLog((NSString *)&__NSConstantStringImpl__var_folders_04_xwbq8q6n0p1dmhhd6y51_vbc0000gp_T_main_0048d2_mi_0);
}
__blockTest_block_func_0就是Block的执行时调用的函数,参数是一个__blockTest_block_impl_0类型的指针。
blockTestvoid blockTest()
{
void (*block)(void) = ((void (*)())&__blockTest_block_impl_0((void *)__blockTest_block_func_0, &__blockTest_block_desc_0_DATA));
((void (*)(__block_impl *)) ((__block_impl *)block)->FuncPtr)((__block_impl *)block);
}
第一句就是定义一个block指针,指向一个通过__blockTest_block_impl_0构造函数构造出的实例对象。
第二句就是调用block,通过block->FuncPtr指针找到__blockTest_block_func_0,并转成((void (*)(__block_impl *))类型,最后将block作为参数传给函数调用。
Flags在__block_impl中我们看到Flags。
在这里Block_private.h中可以看到:
// Values for Block_layout->flags to describe block objects
enum {
BLOCK_DEALLOCATING = (0x0001), // runtime
BLOCK_REFCOUNT_MASK = (0xfffe), // runtime
BLOCK_NEEDS_FREE = (1 << 24), // runtime
BLOCK_HAS_COPY_DISPOSE = (1 << 25), // compiler
BLOCK_HAS_CTOR = (1 << 26), // compiler: helpers have C++ code
BLOCK_IS_GC = (1 << 27), // runtime
BLOCK_IS_GLOBAL = (1 << 28), // compiler
BLOCK_USE_STRET = (1 << 29), // compiler: undefined if !BLOCK_HAS_SIGNATURE
BLOCK_HAS_SIGNATURE = (1 << 30), // compiler
BLOCK_HAS_EXTENDED_LAYOUT=(1 << 31) // compiler
};
也就是说,一般情况下,一个 block 的 flags 成员默认设置为 0。如果当 block 需要 Block_copy() 和 Block_release 这类拷贝辅助函数,则会设置成 1 << 25 ,也就是 BLOCK_HAS_COPY_DISPOSE 类型。可以搜索到大量讲述 Block_copy 方法的博文,其中涉及到了 BLOCK_HAS_COPY_DISPOSE 。
总结一下枚举类的用法,前 16 位即起到标记作用,又可记录引用计数
block截获变量BLOCK_DEALLOCATING:释放标记。一般常用 BLOCK_NEEDS_FREE 做 位与 *** 作,一同传入 Flags ,告知该 block 可释放。
BLOCK_REFCOUNT_MASK:一般参与判断引用计数,是一个可选用参数。
BLOCK_NEEDS_FREE:通过设置该枚举位,来告知该 block 可释放。意在说明 block 是 heap block ,即我们常说的 _NSConcreteMallocBlock 。
BLOCK_HAS_COPY_DISPOSE:是否拥有拷贝辅助函数(a copy helper function)。
BLOCK_HAS_CTOR:是否拥有 block 析构函数(dispose function)。
BLOCK_IS_GC:是否启用 GC 机制(Garbage Collection)。
BLOCK_HAS_SIGNATURE:与 BLOCK_USE_STRET 相对,判断是否当前 block 拥有一个签名。用于 runtime 时动态调用。
void blockTest()
{
int num = 10;
void (^block)(void) = ^{
NSLog(@"%d",num);
};
num = 20;
block();
}
int main(int argc, char * argv[]) {
@autoreleasepool {
blockTest();
}
}
打印出来的结果是10,我们来看看c++代码:
struct __blockTest_block_impl_0 {
struct __block_impl impl;
struct __blockTest_block_desc_0* Desc;
int num;
__blockTest_block_impl_0(void *fp, struct __blockTest_block_desc_0 *desc, int _num, int flags=0) : num(_num) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
static void __blockTest_block_func_0(struct __blockTest_block_impl_0 *__cself) {
int num = __cself->num; // bound by copy
NSLog((NSString *)&__NSConstantStringImpl__var_folders_04_xwbq8q6n0p1dmhhd6y51_vbc0000gp_T_main_3c2714_mi_0,num);
}
void blockTest()
{
int num = 10;
void (*block)(void) = ((void (*)())&__blockTest_block_impl_0((void *)__blockTest_block_func_0, &__blockTest_block_desc_0_DATA, num));
num = 20;
((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block);
}
对比一开始的代码,这块代码多了个int num;
而构造函数里也多了个参数,__blockTest_block_impl_0(void *fp, struct __blockTest_block_desc_0 *desc, int _num, int flags=0),所以在调用构造函数的时候,只是想num的值传了过去,这就能解释为什么这里打印出来的是10,而不是20。
void blockTest()
{
static int num = 10;
void (^block)(void) = ^{
NSLog(@"%d",num);
num = 30;
};
num = 20;
block();
NSLog(@"%d",num);
}
打印出来的结果分别是20、30。
C++代码:
struct __blockTest_block_impl_0 {
struct __block_impl impl;
struct __blockTest_block_desc_0* Desc;
int *num;
__blockTest_block_impl_0(void *fp, struct __blockTest_block_desc_0 *desc, int *_num, int flags=0) : num(_num) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
static void __blockTest_block_func_0(struct __blockTest_block_impl_0 *__cself) {
int *num = __cself->num; // bound by copy
NSLog((NSString *)&__NSConstantStringImpl__var_folders_04_xwbq8q6n0p1dmhhd6y51_vbc0000gp_T_main_5a95f6_mi_0,(*num));
(*num) = 30;
}
void blockTest()
{
static int num = 10;
void (*block)(void) = ((void (*)())&__blockTest_block_impl_0((void *)__blockTest_block_func_0, &__blockTest_block_desc_0_DATA, &num));
num = 20;
NSLog((NSString *)&__NSConstantStringImpl__var_folders_04_xwbq8q6n0p1dmhhd6y51_vbc0000gp_T_main_5a95f6_mi_1,num);
((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block);
}
这次和上次又有点不一样,不是int num而是int *num,这里传进来的是num的指针,所以,共用的是同一个变量,所以在block改变了值,在block外面也会发现值改变了。
为什么这里就用指针,而刚刚那里不用指针呢?
因为num是栈上的变量,如果用指针去访问,一旦num的作用域结束了,num被释放了这时候block再通过指针去访问变量就会有问题,就是个野指针。而对于static修饰的就不一样了,不用担心会被释放掉。
int num = 10;
void blockTest()
{
void (^block)(void) = ^{
NSLog(@"%d",num);
num = 30;
};
num = 20;
block();
NSLog(@"%d",num);
}
打印出来的结果也是20、30.
int num = 10;
struct __blockTest_block_impl_0 {
struct __block_impl impl;
struct __blockTest_block_desc_0* Desc;
__blockTest_block_impl_0(void *fp, struct __blockTest_block_desc_0 *desc, int flags=0) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
static void __blockTest_block_func_0(struct __blockTest_block_impl_0 *__cself) {
NSLog((NSString *)&__NSConstantStringImpl__var_folders_04_xwbq8q6n0p1dmhhd6y51_vbc0000gp_T_main_1875c6_mi_0,num);
num = 30;
}
在这里,构造函数并没有把num作为参数传进去,直接使用全局的num。
使用__block修饰变量void blockTest()
{
__block int num = 10;
void (^block)(void) = ^{
NSLog(@"%d",num);
num = 30;
};
num = 20;
block();
NSLog(@"%d",num);
}
clang改写后的代码如下:
struct __Block_byref_num_0 {
void *__isa;
__Block_byref_num_0 *__forwarding;
int __flags;
int __size;
int num;
};
struct __blockTest_block_impl_0 {
struct __block_impl impl;
struct __blockTest_block_desc_0* Desc;
__Block_byref_num_0 *num; // by ref
__blockTest_block_impl_0(void *fp, struct __blockTest_block_desc_0 *desc, __Block_byref_num_0 *_num, int flags=0) : num(_num->__forwarding) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
static void __blockTest_block_func_0(struct __blockTest_block_impl_0 *__cself) {
__Block_byref_num_0 *num = __cself->num; // bound by ref
NSLog((NSString *)&__NSConstantStringImpl__var_folders_04_xwbq8q6n0p1dmhhd6y51_vbc0000gp_T_main_018b76_mi_0,(num->__forwarding->num));
(num->__forwarding->num) = 30;
}
static void __blockTest_block_copy_0(struct __blockTest_block_impl_0*dst, struct __blockTest_block_impl_0*src) {_Block_object_assign((void*)&dst->num, (void*)src->num, 8/*BLOCK_FIELD_IS_BYREF*/);}
static void __blockTest_block_dispose_0(struct __blockTest_block_impl_0*src) {_Block_object_dispose((void*)src->num, 8/*BLOCK_FIELD_IS_BYREF*/);}
static struct __blockTest_block_desc_0 {
size_t reserved;
size_t Block_size;
void (*copy)(struct __blockTest_block_impl_0*, struct __blockTest_block_impl_0*);
void (*dispose)(struct __blockTest_block_impl_0*);
} __blockTest_block_desc_0_DATA = { 0, sizeof(struct __blockTest_block_impl_0), __blockTest_block_copy_0, __blockTest_block_dispose_0};
void blockTest()
{
__attribute__((__blocks__(byref))) __Block_byref_num_0 num = {(void*)0,(__Block_byref_num_0 *)&num, 0, sizeof(__Block_byref_num_0), 10};
void (*block)(void) = ((void (*)())&__blockTest_block_impl_0((void *)__blockTest_block_func_0, &__blockTest_block_desc_0_DATA, (__Block_byref_num_0 *)&num, 570425344));
(num.__forwarding->num) = 20;
((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block);
NSLog((NSString *)&__NSConstantStringImpl__var_folders_04_xwbq8q6n0p1dmhhd6y51_vbc0000gp_T_main_018b76_mi_1,(num.__forwarding->num));
}
我们可以先从blockTest开始看:
__Block_byref_num_0__attribute__((__blocks__(byref))) __Block_byref_num_0 num = {(void*)0,(__Block_byref_num_0 *)&num, 0, sizeof(__Block_byref_num_0), 10};
这里定义了一个__Block_byref_num_0的实例,再看看这个实例有什么:
struct __Block_byref_num_0 {
void *__isa;
__Block_byref_num_0 *__forwarding;
int __flags;
int __size;
int num;
};
有一个isa指针,有一个指向和自己类型一样的__forwarding指针,还有一个flags、size,最后,会有一个num,num估计就是保存着变量的值。至于__forwarding指针,后面会讲到。
__blockTest_block_copy_0和__blockTest_block_dispose_0__blockTest_block_copy_0中调用的是_Block_object_assign,__blockTest_block_dispose_0中调用的是_Block_object_dispose。
调用时机:
__blockTest_block_copy_0: __block变量结构体实例从栈拷贝到堆时
__blockTest_block_dispose_0:__block变量结构体实例引用计数为0时
// Runtime support functions used by compiler when generating copy/dispose helpers
// Values for _Block_object_assign() and _Block_object_dispose() parameters
enum {
// see function implementation for a more complete description of these fields and combinations
BLOCK_FIELD_IS_OBJECT = 3, // id, NSObject, __attribute__((NSObject)), block, ...
BLOCK_FIELD_IS_BLOCK = 7, // a block variable
BLOCK_FIELD_IS_BYREF = 8, // the on stack structure holding the __block variable
BLOCK_FIELD_IS_WEAK = 16, // declared __weak, only used in byref copy helpers
BLOCK_BYREF_CALLER = 128, // called from __block (byref) copy/dispose support routines.
};
Block的内存管理BLOCK_FIELD_IS_OBJECT:OC对象类型
BLOCK_FIELD_IS_BLOCK:是一个block
BLOCK_FIELD_IS_BYREF:在栈上被__block修饰的变量
BLOCK_FIELD_IS_WEAK:被__weak修饰的变量,只在Block_byref管理内部对象内存时使用
BLOCK_BYREF_CALLER:处理Block_byref内部对象内存的时候会加的一个额外标记(告诉内部实现不要进行retain或者copy)
其实Block总共有三种类型:
分为全局 Block(_NSConcreteGlobalBlock)、栈 Block(_NSConcreteStackBlock)、堆 Block(_NSConcreteMallocBlock)三种形式
在ARC环境下,有哪些情况编译器会自动将栈上的把Block从栈上复制到堆上呢?
以上四种情况会把Block从栈复制到堆上。
当Block复制到堆上,__block修饰的变量也会跟着变化:
1、当Block在栈上的时候,__block变量也会在栈上,被栈上的Block持有着。
2、当Block被复制到堆上时,会通过调用Block内部的copy函数,copy函数内部会调用_Block_object_assign函数。此时__block变量的存储域是堆,__block变量被堆上的Block持有。
3、当堆上的Block被释放,会调用Block内部的dispose,dispose函数内部会调用_Block_object_dispose,堆上的__block被释放。
1、当多个栈上的Block使用栈上的__block变量,__block变量被栈上的多个Block持有。
2、当Block0被复制到堆上时,__block也会被复制到堆上,被堆上Block0持有。Block1仍然持有栈上的__block,原栈上__block变量的__forwarding指向拷贝到堆上之后的__block变量。
3、当Block1也被复制到堆上时,堆上的__block被堆上的Block0和Block1只有,并且__block的引用计数+1。
4、当堆上的Block都被释放,__block变量结构体实例引用计数为0,调用_Block_object_dispose,堆上的__block被释放。
下面来描述__forwarding指针的变化:
一开始,在栈上的forwarding指针是指向自身,当复制到堆以后,forwarding指针指向堆上的__Block_byref_num_0。
forwarding存在意义就是:
__forwarding 保证在栈上或者堆上都能正确访问对应变量
接下来,举个例子
int main(int argc, char * argv[]) {
int num = 10;
NSLog(@"%@",[^{
NSLog(@"%d",num);
} class]);
void (^block)(void) = ^{
NSLog(@"%d",num);
};
NSLog(@"%@",[block class]);
}
打印的结果分别是:
NSStackBlock、NSMallocBlock
因为第一个block没有copy,所以只是栈上面的block,而第二个block是进行了copy,所以是堆上的block。
@interface Person : NSObject
@property (nonatomic, strong) NSString *name;
@end
@implementation Person
- (void)dealloc {
NSLog(@"-------dealloc-------");
}
@end
typedef void(^Block)(void);
int main(int argc, char * argv[]) {
{
Person *person = [[Person alloc] init];
person.name = @"roy";
NSLog(@"%@",[^{
NSLog(@"%@",person.name);
} class]);
NSLog(@"%@",@"+++++++++++++");
}
NSLog(@"%@",@"------------");
}
打印结果:
2022-01-04 22:50:00.490032+0800 HelloWorld[11404:3495395] __NSStackBlock__
2022-01-04 22:50:00.490791+0800 HelloWorld[11404:3495395] +++++++++++++
2022-01-04 22:50:00.490904+0800 HelloWorld[11404:3495395] -------dealloc-------
2022-01-04 22:50:00.491035+0800 HelloWorld[11404:3495395] ------------
我们看到栈上的block内部访问了局部变量,是不会对局部变量强引用的,局部变量还是能释放掉。
strong对象@interface Person : NSObject
@property (nonatomic, strong) NSString *name;
@end
@implementation Person
- (void)dealloc {
NSLog(@"-------dealloc-------");
}
@end
typedef void(^Block)(void);
int main(int argc, char * argv[]) {
Block block;
{
Person *person = [[Person alloc] init];
person.name = @"roy";
block = ^{
NSLog(@"%@",person.name);
};
person.name = @"david";
NSLog(@"%@",@"+++++++++++++");
}
NSLog(@"%@",@"------------");
NSLog(@"%@",[block class]);
block ();
block = NULL;
}
打印结果:
2022-01-04 22:59:32.072849+0800 HelloWorld[11771:3503869] +++++++++++++
2022-01-04 22:59:32.076322+0800 HelloWorld[11771:3503869] ------------
2022-01-04 22:59:32.076494+0800 HelloWorld[11771:3503869] __NSMallocBlock__
2022-01-04 22:59:32.076629+0800 HelloWorld[11771:3503869] david
2022-01-04 22:59:32.077177+0800 HelloWorld[11771:3503869] -------dealloc-------
Clang改写代码:
struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
Person *__strong person;
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, Person *__strong _person, int flags=0) : person(_person) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
__main_block_impl_0里面多了个Person *__strong person。
这里Block截获了局部变量,当Block被拷贝到堆上的时候,使用了强引用指向这个person对象,这就是为什么只有block=NULL的时候(block会被释放),才会打印dealloc方法。
typedef void(^Block)(void);
int main(int argc, char * argv[]) {
Block block;
{
Person *person = [[Person alloc] init];
person.name = @"roy";
__weak Person *weakPerson = person;
block = ^{
NSLog(@"%@",weakPerson.name);
};
weakPerson.name = @"david";
NSLog(@"%@",@"+++++++++++++");
}
NSLog(@"%@",@"------------");
block ();
}
打印结果:
2022-01-04 23:05:53.482355+0800 HelloWorld[11992:3508889] +++++++++++++
2022-01-04 23:05:53.482986+0800 HelloWorld[11992:3508889] -------dealloc-------
2022-01-04 23:05:53.483113+0800 HelloWorld[11992:3508889] ------------
2022-01-04 23:05:53.483217+0800 HelloWorld[11992:3508889] (null)
Clang改写以后的代码:
struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
Person *__weak weakPerson;
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, Person *__weak _weakPerson, int flags=0) : weakPerson(_weakPerson) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
在这里,__main_block_impl_0中多了个Person *__weak weakPerson成员变量。
总结:
当Block内部访问了对象类型的局部变量对象时,如果Block是在栈上,将不会对auto对象产生强引用。如果block被拷贝到堆上,会调用Block内部的copy函数,copy函数内部会调用_Block_object_assign函数,_Block_object_assign会根据auto对象的修饰符(__strong,__weak,__unsafe_unretained)做出相应的 *** 作,当使用的是__strong时,将会对person对象的引用计数加1,当为__weak时,引用计数不变。如果Block从对上移除,会调用block内部的dispose函数,内部会调用_Block_object_dispose函数,这个函数会自动释放引用的局部变量对象。 block循环引用
@interface Person : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, copy) void (^block)(void);
- (void)testReferenceSelf;
@end
@implementation Person
- (void)testReferenceSelf {
self.block = ^ {
NSLog(@"self.name = %s", self.name.UTF8String);
};
self.block();
}
- (void)dealloc {
NSLog(@"-------dealloc-------");
}
@end
int main(int argc, char * argv[]) {
Person *person = [[Person alloc] init];
person.name = @"roy";
[person testReferenceSelf];
}
这是典型的循环引用,person的dealloc方法没有执行。
clang改写后的代码如下:
struct __Person__testReferenceSelf_block_impl_0 {
struct __block_impl impl;
struct __Person__testReferenceSelf_block_desc_0* Desc;
Person *const __strong self;
__Person__testReferenceSelf_block_impl_0(void *fp, struct __Person__testReferenceSelf_block_desc_0 *desc, Person *const __strong _self, int flags=0) : self(_self) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
static void _I_Person_testReferenceSelf(Person * self, SEL _cmd) {
((void (*)(id, SEL, void (*)()))(void *)objc_msgSend)((id)self, sel_registerName("setBlock:"), ((void (*)())&__Person__testReferenceSelf_block_impl_0((void *)__Person__testReferenceSelf_block_func_0, &__Person__testReferenceSelf_block_desc_0_DATA, self, 570425344)));
((void (*(*)(id, SEL))())(void *)objc_msgSend)((id)self, sel_registerName("block"))();
}
这里,__Person__testReferenceSelf_block_impl_0有一个Person *const __strong self;
在person中block强引用了self,而self又强引用了block,导致了循环引用。
@implementation Person
- (void)testReferenceSelf {
__weak typeof(self) weakself = self;
self.block = ^ {
__strong typeof(self) strongself = weakself;
NSLog(@"self.name = %s", strongself.name.UTF8String);
};
self.block();
}
- (void)dealloc {
NSLog(@"-------dealloc-------");
}
@end
打印结果里面会打印出dealloc,person对象被正常释放了。
clang改写后的代码如下:
struct __Person__testReferenceSelf_block_impl_0 {
struct __block_impl impl;
struct __Person__testReferenceSelf_block_desc_0* Desc;
Person *const __weak weakself;
__Person__testReferenceSelf_block_impl_0(void *fp, struct __Person__testReferenceSelf_block_desc_0 *desc, Person *const __weak _weakself, int flags=0) : weakself(_weakself) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
static void _I_Person_testReferenceSelf(Person * self, SEL _cmd) {
__attribute__((objc_ownership(weak))) typeof(self) weakself = self;
((void (*)(id, SEL, void (*)()))(void *)objc_msgSend)((id)self, sel_registerName("setBlock:"), ((void (*)())&__Person__testReferenceSelf_block_impl_0((void *)__Person__testReferenceSelf_block_func_0, &__Person__testReferenceSelf_block_desc_0_DATA, weakself, 570425344)));
((void (*(*)(id, SEL))())(void *)objc_msgSend)((id)self, sel_registerName("block"))();
}
此时,block中对self只是个弱引用。
在Person对象的Dealloc方法中会调用weak引用的处理方法,从weak_table中寻找弱引用的依赖对象,进行清除处理。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)