美文网首页
iOS中dealloc实现机制

iOS中dealloc实现机制

作者: 兰帕德 | 来源:发表于2021-12-29 21:35 被阅读0次
    一、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执行流程结束。

    相关文章

      网友评论

          本文标题:iOS中dealloc实现机制

          本文链接:https://www.haomeiwen.com/subject/oqjbqrtx.html