源碼:objc4-723,地址:https://opensource.apple.com/tarballs/objc4/
調(diào)用流程:首先調(diào)用 _objc_rootDealloc() -> 接下來(lái)調(diào)用 rootDealloc() -> object_dispose()-> clearDeallocating()
一泞辐、調(diào)用 _objc_rootDealloc()
// Replaced by NSZombies
- (void)dealloc {
_objc_rootDealloc(self);
}
二煤辨、調(diào)用 rootDealloc()
void
_objc_rootDealloc(id obj)
{
assert(obj);
obj->rootDealloc();
}
inline void
objc_object::rootDealloc()
{
if (isTaggedPointer()) return; // fixme necessary?
if (fastpath(isa.nonpointer && //優(yōu)化過(guò)isa指針
!isa.weakly_referenced && //不存在弱引用指向
!isa.has_assoc && //沒(méi)設(shè)置過(guò)關(guān)聯(lián)對(duì)象
!isa.has_cxx_dtor && // 沒(méi)有c++的析構(gòu)函數(shù)(.cxx_destruct)
!isa.has_sidetable_rc))// 不存在引用計(jì)數(shù)器是否過(guò)大無(wú)法存儲(chǔ)在isa中(使用 sidetable 來(lái)存儲(chǔ)引用計(jì)數(shù))
{
assert(!sidetable_present());
free(this);
}
else {
object_dispose((id)this);
}
}
2.1、首先判斷 isTaggedPointer 是否是標(biāo)記指針 是直接 return ;
注:Tagged Pointer技術(shù)诀拭,用于優(yōu)化NSNumber迁筛、NSDate、NSString等小對(duì)象的存儲(chǔ)(http://www.reibang.com/p/70551a5e77c0)
2.2耕挨、其次判斷該對(duì)象是否可以被快速釋放细卧。一共有5個(gè)判斷依據(jù):
nonpointer 是否優(yōu)化過(guò)isa指針(類似Tagger Pointer)
weakly_reference 是否存在弱引用指向
has_assoc 是否設(shè)置過(guò)關(guān)聯(lián)對(duì)象
has_cxx_dtor 是否有c++的析構(gòu)函數(shù)(.cxx_destruct)
has_sidetable_rc 引用計(jì)數(shù)器是否過(guò)大無(wú)法存儲(chǔ)在isa中(使用 sidetable 來(lái)存儲(chǔ)引用計(jì)數(shù))
三、 object_dispose()
如果不能快速釋放筒占,則調(diào)用 object_dispose()方法贪庙,做下一步的處理(調(diào)用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(); //是否存在析構(gòu)函數(shù)(“清理善后” 的工作的函數(shù))
bool assoc = obj->hasAssociatedObjects(); //是否有關(guān)聯(lián)對(duì)象
// This order is important.
if (cxx) object_cxxDestruct(obj);//(Calls C++ destructors.)c++的銷毀器來(lái)銷毀成員變量
if (assoc) _object_remove_assocations(obj);//(Calls ARC ivar cleanup.)用來(lái)釋放動(dòng)態(tài)綁定的對(duì)象
obj->clearDeallocating();
}
return obj;
}
3.1、判斷是否存在c++的析構(gòu)函數(shù)翰苫,有則調(diào)用object_cxxDestruct()
3.1.1止邮、object_cxxDestruct()
object_cxxDestruct這個(gè)方法最終會(huì)調(diào)用objc_storeStrong來(lái)釋放成員變量(實(shí)例變量)
3.2、移除關(guān)聯(lián)對(duì)象_object_remove_assocations(常用于category中添加帶變量的屬性)奏窑,關(guān)聯(lián)對(duì)象定義:https://www.cnblogs.com/limicheng/p/4186214.html
3.3导披、調(diào)用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,并最終都調(diào)用:weak_clear_no_lock埃唯,該方法將所有指向所提供對(duì)象的所有弱指針置清空撩匕。
總結(jié):clearDeallocating一共做了兩件事
將對(duì)象弱引用表清空,即將弱引用該對(duì)象的指針置為nil
清空引用計(jì)數(shù)表(當(dāng)一個(gè)對(duì)象的引用計(jì)數(shù)值過(guò)大(超過(guò)255)時(shí)墨叛,引用計(jì)數(shù)會(huì)存儲(chǔ)在一個(gè)叫 SideTable 的屬性中止毕,此時(shí)isa的 has_sidetable_rc 值為1),這就是為什么弱引用不會(huì)導(dǎo)致循環(huán)引用的原因