一 : __weak 与 __unsafe_unretained

内存图

解决方式 : __weak
typeof(perosn) 与上面相同,编译器特性.

内存图

解决方式 : __unsafe_unretained

内存图 与weak一样
二 : __weak 与 __unsafe_unretained 区别
__weak :
引用的对象挂掉,其指针置为nil

____unsafe_unretained:
还存在挂掉对象的地址,访问后会产生野指针.
三 :__block解决循环引用问题

源码

内存结构图
weakSelf 相当于 __block_byref_person这个结构体变量的 person置为nill,循环链条断裂,彼此不再循环引用.

四: MRC下
解决循环引用问题
MRC下__Block_byref结构体对象不会对里面的person进行强引用.是弱引用

MRC 不支持__weak,所有有两种解决方案

保证编译通过,确保在使用person时,不会挂掉

网友评论