1. 循环引用是如何产生的
循环引用顾名思义就是相互直接或间接强引用达成了一个环,造成了相互持有(eg:A--B B--A),释放的时候由于这个持有关系,A要等待B释放了对自己的持有才会释放,同时B也要等待A释放了对自己的持有才会释放,最终导致都无法释放,产生了内存泄漏
常见的循环引用的产生场景
- 对象的Block成员内部访问了self或者self的属性
- 代理的修饰符使用了强引用,例如:strong
- 对象A持有了对象B,对象B直接或间接的持有了对象A
2. weak的方式解决循环引用
weak是如何解决循环引用的细节可以参照这个文章weak如何解决循环引用
@property (copy, nonatomic) void(^testBreakRetainCycleBlockByWeak)(void);
- (void)testBreakRetainCycleByWeak {
__weak typeof(self) weakSelf = self;
self.testBreakRetainCycleBlockByWeak = ^(void) {
__strong typeof(self) strongSelf = weakSelf; // 涉及到多线程异步操作,最好加上一个强引用,这样保证下面的任务可以被执行
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"%@", strongSelf); // 执行该段代码之后,临时变量strongSelf就释放了,self就能正常的dealloc
});
};
self.testBlockTypeBlock();
}
使用weak,block内部结构捕获变量使用的是弱引用关系,这样就避免了相互强引用
3. 临时变量持有self,设置为nil
@property (copy, nonatomic) void(^testBreakRetainCycleBlockBySetNil)(void);
- (void)testBreakRetainCycleBySetNil {
NSLog(@"testBreakRetainCycleBySetNil:self.retainCount before tmp refrence = %ld", CFGetRetainCount((__bridge CFTypeRef)(self))); // 1
__block BlockTest *strongSelf = self;
NSLog(@"testBreakRetainCycleBySetNil:self.retainCount after tmp refrence = %ld", CFGetRetainCount((__bridge CFTypeRef)(self))); // 2
self.testBreakRetainCycleBlockBySetNil = ^(void) {
NSLog(@"testBreakRetainCycle:%@", strongSelf);
strongSelf = nil;
};
self.testBreakRetainCycleBlockBySetNil();
NSLog(@"testBreakRetainCycleBySetNil:self.retainCount after set nil = %ld", CFGetRetainCount((__bridge CFTypeRef)(self))); // 1
}
Clang Rewrite 查看内部实现
Clang rewrite可以参照weak如何解决循环引用
struct __Block_byref_strongSelf_3 {
void *__isa;
__Block_byref_strongSelf_3 *__forwarding;
int __flags;
int __size;
void (*__Block_byref_id_object_copy)(void*, void*);
void (*__Block_byref_id_object_dispose)(void*);
BlockTest *__strong strongSelf;
};
struct __BlockTest__testBreakRetainCycleBySetNil_block_impl_0 {
struct __block_impl impl;
struct __BlockTest__testBreakRetainCycleBySetNil_block_desc_0* Desc;
__Block_byref_strongSelf_3 *strongSelf; // by ref
__BlockTest__testBreakRetainCycleBySetNil_block_impl_0(void *fp, struct __BlockTest__testBreakRetainCycleBySetNil_block_desc_0 *desc, __Block_byref_strongSelf_3 *_strongSelf, int flags=0) : strongSelf(_strongSelf->__forwarding) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
static void __BlockTest__testBreakRetainCycleBySetNil_block_func_0(struct __BlockTest__testBreakRetainCycleBySetNil_block_impl_0 *__cself) {
__Block_byref_strongSelf_3 *strongSelf = __cself->strongSelf; // bound by ref
NSLog((NSString *)&__NSConstantStringImpl__var_folders_8j_551wkz1d1rg878kt8mg21tg40000gn_T_BlockTest_04791f_mi_25, (strongSelf->__forwarding->strongSelf));
(strongSelf->__forwarding->strongSelf) = __null;
}
static void __BlockTest__testBreakRetainCycleBySetNil_block_copy_0(struct __BlockTest__testBreakRetainCycleBySetNil_block_impl_0*dst, struct __BlockTest__testBreakRetainCycleBySetNil_block_impl_0*src) {_Block_object_assign((void*)&dst->strongSelf, (void*)src->strongSelf, );}
static void __BlockTest__testBreakRetainCycleBySetNil_block_dispose_0(struct __BlockTest__testBreakRetainCycleBySetNil_block_impl_0*src) {_Block_object_dispose((void*)src->strongSelf,);}
static struct __BlockTest__testBreakRetainCycleBySetNil_block_desc_0 {
size_t reserved;
size_t Block_size;
void (*copy)(struct __BlockTest__testBreakRetainCycleBySetNil_block_impl_0*, struct __BlockTest__testBreakRetainCycleBySetNil_block_impl_0*);
void (*dispose)(struct __BlockTest__testBreakRetainCycleBySetNil_block_impl_0*);
} __BlockTest__testBreakRetainCycleBySetNil_block_desc_0_DATA = { 0, sizeof(struct __BlockTest__testBreakRetainCycleBySetNil_block_impl_0), __BlockTest__testBreakRetainCycleBySetNil_block_copy_0, __BlockTest__testBreakRetainCycleBySetNil_block_dispose_0};
static void _I_BlockTest_testBreakRetainCycleBySetNil(BlockTest * self, SEL _cmd) {
__attribute__((__blocks__(byref))) __Block_byref_strongSelf_3 strongSelf = {(void*)0,(__Block_byref_strongSelf_3 *)&strongSelf, 33554432, sizeof(__Block_byref_strongSelf_3), __Block_byref_id_object_copy_131, __Block_byref_id_object_dispose_131, self};
((void (*)(id, SEL, void (*)()))(void *)objc_msgSend)((id)self, sel_registerName("setTestBreakRetainCycleBlockBySetNil:"), ((void (*)())&__BlockTest__testBreakRetainCycleBySetNil_block_impl_0((void *)__BlockTest__testBreakRetainCycleBySetNil_block_func_0, &__BlockTest__testBreakRetainCycleBySetNil_block_desc_0_DATA, (__Block_byref_strongSelf_3 *)&strongSelf, 570425344)));
((void (*(*)(id, SEL))())(void *)objc_msgSend)((id)self, sel_registerName("testBreakRetainCycleBlockBySetNil"))();
}
这种方式可以解决循环引用:由于定义了block类型的局部变量,会转换成结构体(结构体内部强持有了self),block函数内部结构体中引用这个结构体,当将block类型的局部变量置为nil之后,强持有关系就被打断了
可以看到strongSelf = nil;
内部是将Block的结构体持有的strongSelf置为空,那么block就没有强持有self了,引用环打断了
(strongSelf->__forwarding->strongSelf) = __null;
4. 将self作为参数传递的方式
@property (copy, nonatomic) void(^testBreakRetainCycleBlockBySetNil)(void);
- (void)testBreakRetainCycleByPassParam {
self.testBreakRetainCycleBlockByPassParam = ^(BlockTest *paramSelf) {
NSLog(@"testBreakRetainCycle:%@", paramSelf);
};
self.testBreakRetainCycleBlockByPassParam(self);
}
Clang Rewrite 查看内部实现
static void _I_BlockTest_testBlockRetainCycle(BlockTest * self, SEL _cmd) {
((void (*)(id, SEL, void (*)()))(void *)objc_msgSend)((id)self, sel_registerName("setTestRetainCycleBlock:"), ((void (*)())&__BlockTest__testBlockRetainCycle_block_impl_0((void *)__BlockTest__testBlockRetainCycle_block_func_0, &__BlockTest__testBlockRetainCycle_block_desc_0_DATA, self, 570425344)));
((void (*(*)(id, SEL))())(void *)objc_msgSend)((id)self, sel_registerName("testRetainCycleBlock"))();
}
struct __BlockTest__testBreakRetainCycleByPassParam_block_impl_0 {
struct __block_impl impl;
struct __BlockTest__testBreakRetainCycleByPassParam_block_desc_0* Desc;
__BlockTest__testBreakRetainCycleByPassParam_block_impl_0(void *fp, struct __BlockTest__testBreakRetainCycleByPassParam_block_desc_0 *desc, int flags=0) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
static void __BlockTest__testBreakRetainCycleByPassParam_block_func_0(struct __BlockTest__testBreakRetainCycleByPassParam_block_impl_0 *__cself, BlockTest *__strong paramSelf) {
NSLog((NSString *)&__NSConstantStringImpl__var_folders_r0_4tb84bbj15j3kbzrnk8hqdwm0000gn_T_BlockTest_0cf65b_mi_24, paramSelf);
}
static struct __BlockTest__testBreakRetainCycleByPassParam_block_desc_0 {
size_t reserved;
size_t Block_size;
} __BlockTest__testBreakRetainCycleByPassParam_block_desc_0_DATA = { 0, sizeof(struct __BlockTest__testBreakRetainCycleByPassParam_block_impl_0)};
static void _I_BlockTest_testBreakRetainCycleByPassParam(BlockTest * self, SEL _cmd) {
((void (*)(id, SEL, void (*)(BlockTest *__strong)))(void *)objc_msgSend)((id)self, sel_registerName("setTestBreakRetainCycleBlockByPassParam:"), ((void (*)(BlockTest *__strong))&__BlockTest__testBreakRetainCycleByPassParam_block_impl_0((void *)__BlockTest__testBreakRetainCycleByPassParam_block_func_0, &__BlockTest__testBreakRetainCycleByPassParam_block_desc_0_DATA)));
((void (*(*)(id, SEL))(BlockTest *__strong))(void *)objc_msgSend)((id)self, sel_registerName("testBreakRetainCycleBlockByPassParam"))(self);
}
rewrite之后,查看传参的方式,内部不会捕获(持有)这个参数,当block执行完毕参数就被释放了
这种传参的方式,不会有循环引用问题:由于传参的方式,block内部不会去捕获持有这个参数,只是一个临时变量的强引用,当block执行完之后,参数就会被释放