先看一下delloc的具體實(shí)現(xiàn):
dealloc的實(shí)現(xiàn)
- (void)dealloc {
_objc_rootDealloc(self);
}
進(jìn)入_objc_rootDealloc
void
_objc_rootDealloc(id obj)
{
assert(obj);
obj->rootDealloc();
}
進(jìn)入rootDealloc
inline void
objc_object::rootDealloc()
{
if (isTaggedPointer()) return; // fixme necessary?
//如果是優(yōu)化的isa
//并且對應(yīng)的沒有關(guān)聯(lián)對象、weak應(yīng)用谐宙、以及c++析構(gòu)函數(shù)和sideTable的引用計(jì)數(shù)他挎,就直接free掉
if (fastpath(isa.nonpointer &&
!isa.weakly_referenced &&
!isa.has_assoc &&
!isa.has_cxx_dtor &&
!isa.has_sidetable_rc))
{
assert(!sidetable_present());
free(this);
}
else {
//進(jìn)入銷毀流程
object_dispose((id)this);
}
}
進(jìn)入object_dispose
id
object_dispose(id obj)
{
if (!obj) return nil;
objc_destructInstance(obj);
free(obj);
return nil;
}
接著進(jìn)入objc_destructInstance
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);//清除成員變量
if (assoc) _object_remove_assocations(obj);//移除關(guān)聯(lián)對象
obj->clearDeallocating();//將指向當(dāng)前的弱指針置為nil
}
return obj;
}
weak指針置nil
當(dāng)weak引用指向的對象被釋放時筝尾,我們需要把指針置為nil
我們在前面已經(jīng)講解了,當(dāng)一個對象釋放時办桨,需要調(diào)用objc_release方法筹淫,如果引用計(jì)數(shù)為0時,會執(zhí)行dealloc方法呢撞,在把weak指針置nil的過程會調(diào)用clearDeallocating
void
objc_clear_deallocating(id obj)
{
assert(obj);
if (obj->isTaggedPointer()) return;
obj->clearDeallocating();
}
- 在函數(shù)內(nèi)部調(diào)用了clearDeallocating函數(shù)
inline void
objc_object::clearDeallocating()
{
//如果是沒有優(yōu)化過的isa指針
if (slowpath(!isa.nonpointer)) {
// Slow path for raw pointer isa.
sidetable_clearDeallocating();
}
//是優(yōu)化過的isa指針损姜,并且是否有弱引用過饰剥,或者引用計(jì)數(shù)是否存在sideTable中
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());
}
- 不論我們是哪種方式,我們都會調(diào)用weak_clear_no_lock方法
void
weak_clear_no_lock(weak_table_t *weak_table, id referent_id)
{
//1摧阅、拿到被銷毀對象的指針
objc_object *referent = (objc_object *)referent_id;
//2汰蓉、通過 指針 在weak_table中查找出對應(yīng)的entry
weak_entry_t *entry = weak_entry_for_referent(weak_table, referent);
if (entry == nil) {
return;
}
//3、將所有的引用設(shè)置成nil
weak_referrer_t *referrers;
size_t count;
if (entry->out_of_line()) {
//3.1逸尖、如果弱引用超過4個則將referrers數(shù)組內(nèi)的弱引用都置成nil古沥。
referrers = entry->referrers;
count = TABLE_SIZE(entry);
}
else {
//3.2、不超過4個則將inline_referrers數(shù)組內(nèi)的弱引用都置成nil
referrers = entry->inline_referrers;
count = WEAK_INLINE_COUNT;
}
//循環(huán)設(shè)置所有的引用為nil
for (size_t i = 0; i < count; ++i) {
objc_object **referrer = referrers[i];
if (referrer) {
if (*referrer == referent) {
*referrer = nil;
}
else if (*referrer) {
_objc_inform("__weak variable at %p holds %p instead of %p. "
"This is probably incorrect use of "
"objc_storeWeak() and objc_loadWeak(). "
"Break on objc_weak_error to debug.\n",
referrer, (void*)*referrer, (void*)referent);
objc_weak_error();
}
}
}
//4娇跟、從weak_table中移除entry
weak_entry_remove(weak_table, entry);
}