if (fastpath(isa.nonpointer && // 0 普通指針 存放 類對象、元類對象內存地址
!isa.weakly_referenced && //無弱引用
!isa.has_assoc && //無關聯對象
!isa.has_cxx_dtor && //無cxx析構函數
!isa.has_sidetable_rc)) //無散列表引用計數
{
assert(!sidetable_present());
free(this); //直接釋放
}
else {
object_dispose((id)this);//則作其餘操做
}
複製代碼
objc_destructInstance
是否有c++析構函數
是否存在關聯對象
obj->clearDeallocating();
free
複製代碼
weak_clear_no_lockc++
clearDeallocating函數首先根據對象地址獲取全部weak指針地址的數組,而後遍歷這個數組把其中的數據設爲nil,最後把這個entry從weak表中刪除,最後清理對象的記錄。數組
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());
}
複製代碼
objc_object::sidetable_clearDeallocating()
{
SideTable& table = SideTables()[this];
// clear any weak table items 清理weak 表
// clear extra retain count and deallocating bit 清除引用計數
table.lock();
RefcountMap::iterator it = table.refcnts.find(this);
if (it != table.refcnts.end()) {
if (it->second & SIDE_TABLE_WEAKLY_REFERENCED) {
weak_clear_no_lock(&table.weak_table, (id)this);
}
table.refcnts.erase(it);
}
table.unlock();
}
複製代碼
objc_object::clearDeallocating_slow()
{
ASSERT(isa.nonpointer && (isa.weakly_referenced || isa.has_sidetable_rc));
SideTable& table = SideTables()[this];
table.lock();
if (isa.weakly_referenced) {
weak_clear_no_lock(&table.weak_table, (id)this);
}
if (isa.has_sidetable_rc) {
table.refcnts.erase(this);
}
table.unlock();
}
複製代碼