iOS|iOS dealloc底层深入(dealloc做了什么,为什么弱引用不会导致循环引用)

源码:objc4-723,地址:https://opensource.apple.com/tarballs/objc4/
调用流程:首先调用 _objc_rootDealloc() -> 接下来调用 rootDealloc() -> object_dispose()-> clearDeallocating()
一、调用 _objc_rootDealloc()

// Replaced by NSZombies - (void)dealloc { _objc_rootDealloc(self); }

二、调用 rootDealloc()
void _objc_rootDealloc(id obj) { assert(obj); obj->rootDealloc(); }inline void objc_object::rootDealloc() { if (isTaggedPointer()) return; // fixme necessary?if (fastpath(isa.nonpointer&&//优化过isa指针 !isa.weakly_referenced&&//不存在弱引用指向 !isa.has_assoc&&//没设置过关联对象 !isa.has_cxx_dtor&& // 没有c++的析构函数(.cxx_destruct) !isa.has_sidetable_rc))// 不存在引用计数器是否过大无法存储在isa中(使用 sidetable 来存储引用计数) { assert(!sidetable_present()); free(this); } else { object_dispose((id)this); } }

2.1、首先判断 isTaggedPointer 是否是标记指针 是直接 return ;
注:Tagged Pointer技术,用于优化NSNumber、NSDate、NSString等小对象的存储(https://www.jianshu.com/p/70551a5e77c0)
2.2、其次判断该对象是否可以被快速释放。一共有5个判断依据:
nonpointer 是否优化过isa指针(类似Tagger Pointer)
weakly_reference 是否存在弱引用指向
has_assoc 是否设置过关联对象
has_cxx_dtor 是否有c++的析构函数(.cxx_destruct)
has_sidetable_rc 引用计数器是否过大无法存储在isa中(使用 sidetable 来存储引用计数)
三、 object_dispose()
如果不能快速释放,则调用 object_dispose()方法,做下一步的处理(调用objc_destructInstance)
/*********************************************************************** * object_dispose * fixme * Locking: none **********************************************************************/ id object_dispose(id obj) { if (!obj) return nil; objc_destructInstance(obj); free(obj); return nil; }

/*********************************************************************** * objc_destructInstance * Destroys an instance without freeing memory. * Calls C++ destructors. * Calls ARC ivar cleanup. * Removes associative references. * Returns `obj`. Does nothing if `obj` is nil. **********************************************************************/ void *objc_destructInstance(id obj) { if (obj) { // Read all of the flags at once for performance. bool cxx = obj->hasCxxDtor(); //是否存在析构函数(“清理善后” 的工作的函数) bool assoc = obj->hasAssociatedObjects(); //是否有关联对象// This order is important. if (cxx) object_cxxDestruct(obj); //(Calls C++ destructors.)c++的销毁器来销毁成员变量 if (assoc) _object_remove_assocations(obj); //(Calls ARC ivar cleanup.)用来释放动态绑定的对象 obj->clearDeallocating(); }return obj; }

3.1、判断是否存在c++的析构函数,有则调用object_cxxDestruct()
3.1.1、object_cxxDestruct()
object_cxxDestruct这个方法最终会调用objc_storeStrong来释放成员变量(实例变量)
【iOS|iOS dealloc底层深入(dealloc做了什么,为什么弱引用不会导致循环引用)】3.2、移除关联对象_object_remove_assocations(常用于category中添加带变量的属性),关联对象定义:https://www.cnblogs.com/limicheng/p/4186214.html
3.3、调用clearDeallocating()方法
inline void objc_object::clearDeallocating() { if (slowpath(!isa.nonpointer)) { // Slow path for raw pointer isa. sidetable_clearDeallocating(); } else if (slowpath(isa.weakly_referenced||isa.has_sidetable_rc)) { // Slow path for non-pointer isa with weak refs and/or side table data. clearDeallocating_slow(); }assert(!sidetable_present()); }

sidetable_clearDeallocating和clearDeallocating_slow,并最终都调用:weak_clear_no_lock,该方法将所有指向所提供对象的所有弱指针置清空。
总结:clearDeallocating一共做了两件事
将对象弱引用表清空,即将弱引用该对象的指针置为nil
清空引用计数表(当一个对象的引用计数值过大(超过255)时,引用计数会存储在一个叫 SideTable 的属性中,此时isa的 has_sidetable_rc 值为1),这就是为什么弱引用不会导致循环引用的原因

    推荐阅读