一、dealloc调用流程
- 1、首先调用
_objc_rootDealloc() - 2、然后调用
rootDealloc() - 3、判断是否可以被释放,判断依据为,是否有以下5中情况:
(1)NONPointer_ISA
(2)weakly_reference
(3)has_assoc
(4)has_cxx_dtor
(5)has_sidetable_rc - 4、如果有以上5中情况中的任意一种,则调用
object_dispose()方法;如果没有其中任意一种,表明可以执行释放操作,执行C函数的free()。 - 5、执行完毕。
二、object_dispose()调用流程
- 1、调用
objc_destructInstance() - 2、调用C函数的
free()
三、objc_destructInstance()调用流程
- 1、判断
has_cxx_dtor,如果有C++相关内容,要调用objc_cxxDestruct(),销毁C++相关内容。 - 2、判断
hasAssociatatedObjects,如果有,要调用objc_remove_associations(),销毁关联对象的一系列操作。 - 3、调用
clearDeallocating()。 - 4、执行完毕。
四、clearDeallocating()调用流程
- 1、执行
sideTable_clearDeallocating()。 - 2、执行
weak_clear_no_lock,在这一步骤中,会将指向该对象的弱引用指针置为nil。 - 3、执行
table.refcnts.eraser(),从引用计数表中擦除改对象的引用计数。 - 4、至此,dealloc执行流程结束。