weak實(shí)現(xiàn)原理

平時(shí)開發(fā)中我們經(jīng)常會(huì)用到weak羊精,但是它的實(shí)現(xiàn)原理也許不是很清楚谓厘,今天就從源碼來探究一下(weak實(shí)現(xiàn)原理源碼鏈接)恍箭。

weak指針的建立

weak修飾對(duì)象不增加其引用計(jì)數(shù)挎扰,系統(tǒng)通過一個(gè)hash表來實(shí)現(xiàn)對(duì)象的弱引用。
在Xcode下編寫如下代碼:
__weak obj1 = obj;
編譯器編譯之后變成類似如下的代碼:
objc_initWeak(&obj1,obj);
翻開runtime源碼腰根,NSObject.mm,找到objc_initWeak函數(shù)拓型,實(shí)現(xiàn)如下:

// jack.deng weak實(shí)現(xiàn)原理
id
objc_initWeak(id *location, id newObj)
{
    if (!newObj) {
        *location = nil;
        return nil;
    }

    return storeWeak<DontHaveOld, DoHaveNew, DoCrashIfDeallocating>
        (location, (objc_object*)newObj);
}

apple對(duì)參數(shù)已有說明额嘿,location是weak指針的地址瘸恼,newObj是被指向的對(duì)象的地址。接下來尋找storeWeak函數(shù)册养,先看apple對(duì)storeWeak函數(shù)的注釋:

// Update a weak variable.
// If HaveOld is true, the variable has an existing value 
//   that needs to be cleaned up. This value might be nil.
// If HaveNew is true, there is a new value that needs to be 
//   assigned into the variable. This value might be nil.
// If CrashIfDeallocating is true, the process is halted if newObj is 
//   deallocating or newObj's class does not support weak references. 
//   If CrashIfDeallocating is false, nil is stored instead.

如果weak指針有指向的對(duì)象东帅,那么清除這個(gè)對(duì)象,然后weak指針指向新的對(duì)象球拦;如果weak指針指向新的對(duì)象靠闭,那么就將新的weak引用存起來;如果weak指針指向的對(duì)象被釋放了坎炼,那么就不會(huì)存儲(chǔ)這個(gè)weak引用愧膀,直接返回nil。下面一行行看代碼:

weak實(shí)現(xiàn)原理關(guān)鍵方法

打開NSObject.mm查看storeWeak(id *location, objc_object *newObj)方法實(shí)現(xiàn)谣光。

// jack.deng weak實(shí)現(xiàn)原理關(guān)鍵方法     //[我已經(jīng)配了中英文雙語解釋了]
// HaveOld:  true - 變量有值
//          false - 需要被及時(shí)清理檩淋,當(dāng)前值可能為 nil
// HaveNew:  true - 需要被分配的新值,當(dāng)前值可能為 nil
//          false - 不需要分配新值
// CrashIfDeallocating: true - 說明 newObj 已經(jīng)釋放或者 newObj 不支持弱引用萄金,該過程需要暫停
//          false - 用 nil 替代存儲(chǔ)

// 大家可能看不懂template蟀悦,我簡(jiǎn)單解釋一下,這是c++的模板函數(shù):  
// 模板(Template)指C++程序設(shè)計(jì)設(shè)計(jì)語言中采用類型作為參數(shù)的程序設(shè)計(jì)氧敢,支持通用程序設(shè)計(jì)日戈。簡(jiǎn)單來說就是支持多種類型。
// 我們這里可以認(rèn)為這個(gè)storeWeak函數(shù)有5個(gè)參數(shù)就可以了
template <HaveOld haveOld, HaveNew haveNew,
          CrashIfDeallocating crashIfDeallocating>
static id 
storeWeak(id *location, objc_object *newObj)
{
    assert(haveOld  ||  haveNew);
    if (!haveNew) assert(newObj == nil);

    // 該過程用來更新弱引用指針的指向
    // 初始化 previouslyInitializedClass 指針
    Class previouslyInitializedClass = nil;
    id oldObj;
    // 聲明兩個(gè) SideTable
    SideTable *oldTable;
    SideTable *newTable;

    // Acquire locks for old and new values.
    // Order by lock address to prevent lock ordering problems. 
    // Retry if the old value changes underneath us.
    // 獲得新值和舊值的鎖存位置(用地址作為唯一標(biāo)示)
    // 通過地址來建立索引標(biāo)志孙乖,防止重復(fù)
    // 下面指向的操作會(huì)改變舊值
 retry:
    if (haveOld) {
        // 更改指針浙炼,獲得以 oldObj 為索引所存儲(chǔ)的值地址
        oldObj = *location;
        oldTable = &SideTables()[oldObj];
    } else {
        oldTable = nil;
    }
    if (haveNew) {
        // 更改新值指針,獲得以 newObj 為索引所存儲(chǔ)的值地址
        newTable = &SideTables()[newObj];
    } else {
        newTable = nil;
    }

     // 加鎖操作的圆,防止多線程中競(jìng)爭(zhēng)沖突
    SideTable::lockTwo<haveOld, haveNew>(oldTable, newTable);
    // 避免線程沖突重處理
    // location 應(yīng)該與 oldObj 保持一致鼓拧,如果不同,說明當(dāng)前的 location 已經(jīng)處理過 oldObj 可是又被其他線程所修改
    if (haveOld  &&  *location != oldObj) {
        SideTable::unlockTwo<haveOld, haveNew>(oldTable, newTable);
        goto retry;
    }

    // Prevent a deadlock between the weak reference machinery
    // and the +initialize machinery by ensuring that no 
    // weakly-referenced object has an un-+initialized isa.
    // 防止弱引用間死鎖
    // 并且通過 +initialize 初始化構(gòu)造器保證所有弱引用的 isa 非空指向
    if (haveNew  &&  newObj) {
        // 獲得新對(duì)象的 isa 指針
        Class cls = newObj->getIsa();
        // 判斷 isa 非空且已經(jīng)初始化
        if (cls != previouslyInitializedClass  &&  
            !((objc_class *)cls)->isInitialized()) 
        {
            // 解鎖
            SideTable::unlockTwo<haveOld, haveNew>(oldTable, newTable);
            // 對(duì)其 isa 指針進(jìn)行初始化
            _class_initialize(_class_getNonMetaClass(cls, (id)newObj));

            // If this class is finished with +initialize then we're good.
            // If this class is still running +initialize on this thread 
            // (i.e. +initialize called storeWeak on an instance of itself)
            // then we may proceed but it will appear initializing and 
            // not yet initialized to the check above.
            // Instead set previouslyInitializedClass to recognize it on retry.
            // 如果該類已經(jīng)完成執(zhí)行 +initialize 方法是最理想情況
            // 如果該類 +initialize 在線程中
            // 例如 +initialize 正在調(diào)用 storeWeak 方法
            // 需要手動(dòng)對(duì)其增加保護(hù)策略越妈,并設(shè)置 previouslyInitializedClass 指針進(jìn)行標(biāo)記
            previouslyInitializedClass = cls;

            goto retry;
        }
    }

    // Clean up old value, if any.
    // 清除舊值
    if (haveOld) {
        weak_unregister_no_lock(&oldTable->weak_table, oldObj, location);
    }

    // Assign new value, if any.
    // 分配新值
    if (haveNew) {
        newObj = (objc_object *)
            weak_register_no_lock(&newTable->weak_table, (id)newObj, location, 
                                  crashIfDeallocating);
        // 如果弱引用被釋放 weak_register_no_lock 方法返回 nil
        // 在引用計(jì)數(shù)表中設(shè)置若引用標(biāo)記位
        // weak_register_no_lock returns nil if weak store should be rejected

        // Set is-weakly-referenced bit in refcount table.
        if (newObj  &&  !newObj->isTaggedPointer()) {
            // 弱引用位初始化操作
            // 引用計(jì)數(shù)那張散列表的weak引用對(duì)象的引用計(jì)數(shù)中標(biāo)識(shí)為weak引用
            newObj->setWeaklyReferenced_nolock();
        }

        // Do not set *location anywhere else. That would introduce a race.
        // 之前不要設(shè)置 location 對(duì)象季俩,這里需要更改指針指向
        *location = (id)newObj;
    }
    else {
        // No new value. The storage is not changed.
        // 沒有新值,則無需更改
    }
    
    SideTable::unlockTwo<haveOld, haveNew>(oldTable, newTable);

    return (id)newObj;
}

這里面用到了SideTable,我們看看它到底是啥

SideTable

打開NSObject.mm查看struct SideTable {

struct SideTable {
    spinlock_t slock;  // 防止競(jìng)爭(zhēng)的自旋鎖
    RefcountMap refcnts; // 引用計(jì)數(shù)的 hash 表
    weak_table_t weak_table;  // weak 引用全局 hash 表

    SideTable() {
        memset(&weak_table, 0, sizeof(weak_table));
    }

    ~SideTable() {
        _objc_fatal("Do not delete SideTable.");
    }

    void lock() { slock.lock(); }
    void unlock() { slock.unlock(); }
    void forceReset() { slock.forceReset(); }

    // Address-ordered lock discipline for a pair of side tables.

    template<HaveOld, HaveNew>
    static void lockTwo(SideTable *lock1, SideTable *lock2);
    template<HaveOld, HaveNew>
    static void unlockTwo(SideTable *lock1, SideTable *lock2);
};

對(duì)于 slock 和 refcnts 兩個(gè)成員不用多說梅掠,第一個(gè)是為了防止競(jìng)爭(zhēng)選擇的自旋鎖酌住,第二個(gè)是協(xié)助對(duì)象的 isa 指針的 extra_rc 共同引用計(jì)數(shù)的變量。這里主要看 weak 全局 hash 表的結(jié)構(gòu)與作用阎抒。

weak表

weak表是一個(gè)弱引用表酪我,實(shí)現(xiàn)為一個(gè)weak_table_t結(jié)構(gòu)體,存儲(chǔ)了某個(gè)對(duì)象相關(guān)的的所有的弱引用信息且叁。其定義如下(見objc-weak.h中的struct weak_table_t {定義)

struct weak_table_t {
    // 保存了所有指向指定對(duì)象的 weak 指針
    weak_entry_t *weak_entries;
    // 存儲(chǔ)空間
    size_t    num_entries;
    // 參與判斷引用計(jì)數(shù)輔助量
    uintptr_t mask;
    // hash key 最大偏移值
    uintptr_t max_hash_displacement;
};

這是一個(gè)全局弱引用hash表都哭。使用不定類型對(duì)象的地址作為 key ,用 weak_entry_t 類型結(jié)構(gòu)體對(duì)象作為 value 。其中的 weak_entries成員欺矫,從字面意思上看纱新,即為弱引用表入口。其實(shí)現(xiàn)也是這樣的穆趴。
其中weak_entry_t是存儲(chǔ)在弱引用表中的一個(gè)內(nèi)部結(jié)構(gòu)體脸爱,它負(fù)責(zé)維護(hù)和存儲(chǔ)指向一個(gè)對(duì)象的所有弱引用hash表。其定義如下:

#define WEAK_INLINE_COUNT 4
#define REFERRERS_OUT_OF_LINE 2
struct weak_entry_t {
    DisguisedPtr<objc_object> referent;
    union {
        struct {
            weak_referrer_t *referrers;
            uintptr_t        out_of_line_ness : 2;
            uintptr_t        num_refs : PTR_MINUS_2;
            uintptr_t        mask;
            uintptr_t        max_hash_displacement;
        };
        struct {
            // out_of_line_ness field is low bits of inline_referrers[1]
            weak_referrer_t  inline_referrers[WEAK_INLINE_COUNT];
        };
    };

    bool out_of_line() {
        return (out_of_line_ness == REFERRERS_OUT_OF_LINE);
    }

    weak_entry_t& operator=(const weak_entry_t& other) {
        memcpy(this, &other, sizeof(other));
        return *this;
    }

    weak_entry_t(objc_object *newReferent, objc_object **newReferrer)
        : referent(newReferent)
    {
        inline_referrers[0] = newReferrer;
        for (int i = 1; i < WEAK_INLINE_COUNT; i++) {
            inline_referrers[i] = nil;
        }
    }
};

在 weak_entry_t 的結(jié)構(gòu)中未妹,DisguisedPtr referent 是對(duì)泛型對(duì)象的指針做了一個(gè)封裝簿废,通過這個(gè)泛型類來解決內(nèi)存泄漏的問題。從注釋中寫 out_of_line 成員為最低有效位络它,當(dāng)其為0的時(shí)候族檬, weak_referrer_t 成員將擴(kuò)展為多行靜態(tài) hash table。其實(shí)其中的 weak_referrer_t 是二維 objc_object 的別名酪耕,通過一個(gè)二維指針地址偏移导梆,用下標(biāo)作為 hash 的 key,做成了一個(gè)弱引用散列迂烁。
那么在有效位未生效的時(shí)候看尼,out_of_line 、 num_refs盟步、 mask 藏斩、 max_hash_displacement 有什么作用?以下是筆者自身的猜測(cè):
out_of_line:最低有效位却盘,也是標(biāo)志位狰域。當(dāng)標(biāo)志位 0 時(shí),增加引用表指針緯度黄橘。
num_refs:引用數(shù)值兆览。這里記錄弱引用表中引用有效數(shù)字,因?yàn)槿跻帽硎褂玫氖庆o態(tài) hash 結(jié)構(gòu)塞关,所以需要使用變量來記錄數(shù)目抬探。
mask:計(jì)數(shù)輔助量。
max_hash_displacement:hash 元素上限閥值帆赢。
其實(shí) out_of_line 的值通常情況下是等于零的小压,所以弱引用表總是一個(gè) objc_objective 指針二維數(shù)組。一維 objc_objective 指針可構(gòu)成一張弱引用散列表椰于,通過第三緯度實(shí)現(xiàn)了多張散列表怠益,并且表數(shù)量為 WEAK_INLINE_COUNT 。

總結(jié)一下 StripedMap[] : StripedMap 是一個(gè)模板類瘾婿,在這個(gè)類中有一個(gè) array 成員蜻牢,用來存儲(chǔ) PaddedT 對(duì)象烤咧,并且其中對(duì)于 [] 符的重載定義中,會(huì)返回這個(gè) PaddedT 的 value 成員孩饼,這個(gè) value 就是我們傳入的 T 泛型成員髓削,也就是 SideTable 對(duì)象。在 array 的下標(biāo)中镀娶,這里使用了 indexForPointer 方法通過位運(yùn)算計(jì)算下標(biāo),實(shí)現(xiàn)了靜態(tài)的 Hash Table揪罕。而在 weak_table 中梯码,其成員 weak_entry 會(huì)將傳入對(duì)象的地址加以封裝起來,并且其中也有訪問全局弱引用表的入口好啰。


SideTable結(jié)構(gòu)

下面再說說2個(gè)主要方法

舊對(duì)象解除注冊(cè)操作 weak_unregister_no_lock

該方法主要作用是將舊對(duì)象在 weak_table 中解除 weak 指針的對(duì)應(yīng)綁定轩娶。根據(jù)函數(shù)名,稱之為解除注冊(cè)操作框往。從源碼中鳄抒,可以知道其功能就是從 weak_table 中解除 weak 指針的綁定。而其中的遍歷查詢椰弊,就是針對(duì)于 weak_entry 中的多張弱引用散列表许溅。(見obcj-weak.mm中的weak_unregister_no_lock)

void
weak_unregister_no_lock(weak_table_t *weak_table, id referent_id, 
                        id *referrer_id)
{
    objc_object *referent = (objc_object *)referent_id;
    objc_object **referrer = (objc_object **)referrer_id;

    weak_entry_t *entry;

    if (!referent) return;

    if ((entry = weak_entry_for_referent(weak_table, referent))) {
        remove_referrer(entry, referrer);
        bool empty = true;
        if (entry->out_of_line()  &&  entry->num_refs != 0) {
            empty = false;
        }
        else {
            for (size_t i = 0; i < WEAK_INLINE_COUNT; i++) {
                if (entry->inline_referrers[i]) {
                    empty = false; 
                    break;
                }
            }
        }

        if (empty) {
            weak_entry_remove(weak_table, entry);
        }
    }

    // Do not set *referrer = nil. objc_storeWeak() requires that the 
    // value not change.
}
新對(duì)象添加注冊(cè)操作 weak_register_no_lock

這一步與上一步相反,通過 weak_register_no_lock 函數(shù)把心的對(duì)象進(jìn)行注冊(cè)操作秉版,完成與對(duì)應(yīng)的弱引用表進(jìn)行綁定操作贤重。(見obcj-weak.mm中的weak_register_no_lock)

id 
weak_register_no_lock(weak_table_t *weak_table, id referent_id, 
                      id *referrer_id, bool crashIfDeallocating)
{
    objc_object *referent = (objc_object *)referent_id;
    objc_object **referrer = (objc_object **)referrer_id;

    if (!referent  ||  referent->isTaggedPointer()) return referent_id;

    // ensure that the referenced object is viable
    bool deallocating;
    if (!referent->ISA()->hasCustomRR()) {
        deallocating = referent->rootIsDeallocating();
    }
    else {
        BOOL (*allowsWeakReference)(objc_object *, SEL) = 
            (BOOL(*)(objc_object *, SEL))
            object_getMethodImplementation((id)referent, 
                                           SEL_allowsWeakReference);
        if ((IMP)allowsWeakReference == _objc_msgForward) {
            return nil;
        }
        deallocating =
            ! (*allowsWeakReference)(referent, SEL_allowsWeakReference);
    }

    if (deallocating) {
        if (crashIfDeallocating) {
            _objc_fatal("Cannot form weak reference to instance (%p) of "
                        "class %s. It is possible that this object was "
                        "over-released, or is in the process of deallocation.",
                        (void*)referent, object_getClassName((id)referent));
        } else {
            return nil;
        }
    }

    // now remember it and where it is being stored
    weak_entry_t *entry;
    if ((entry = weak_entry_for_referent(weak_table, referent))) {
        append_referrer(entry, referrer);
    } 
    else {
        weak_entry_t new_entry(referent, referrer);
        weak_grow_maybe(weak_table);
        weak_entry_insert(weak_table, &new_entry);
    }

    // Do not set *referrer. objc_storeWeak() requires that the 
    // value not change.

    return referent_id;
}
初始化弱引用對(duì)象流程一覽

弱引用的初始化,從上文的分析中可以看出清焕,主要的操作部分就在弱引用表的取鍵并蝗、查詢散列、創(chuàng)建弱引用表等操作秸妥,可以總結(jié)出如下的流程圖:


初始化弱引用流程圖

這個(gè)圖中省略了很多情況的判斷滚停,但是當(dāng)聲明一個(gè) __weak 會(huì)調(diào)用上圖中的這些方法。當(dāng)然粥惧, storeWeak 方法不僅僅用在 __weak 的聲明中键畴,在 class 內(nèi)部的操作中也會(huì)常常通過該方法來對(duì) weak 對(duì)象進(jìn)行操作。

weak指針的銷毀

weak指針的建立說完了影晓,我們?cè)賮砜纯此匿N毀镰吵。
再來看weak是在什么時(shí)候銷毀并被置為空的。找到dealloc函數(shù)的實(shí)現(xiàn)挂签,其中dealloc主要調(diào)用了objc_destructInstance函數(shù):

// dealloc調(diào)用_objc_rootDealloc
- (void)dealloc {
    _objc_rootDealloc(self);
}
// _objc_rootDealloc調(diào)用obj->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.weakly_referenced  &&  
                 !isa.has_assoc  &&  
                 !isa.has_cxx_dtor  &&  
                 !isa.has_sidetable_rc))
    {
        assert(!sidetable_present());
        free(this);
    } 
    else {
        object_dispose((id)this);
    }
}

id 
object_dispose(id obj)
{
    if (!obj) return nil;

    objc_destructInstance(obj);    
    free(obj);

    return 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.
// 尋找一個(gè)名為 ._cxx_destruct 的函數(shù)疤祭,這一步主要是由編譯器插入代碼實(shí)現(xiàn),這里銷毀了對(duì)象的實(shí)例變量饵婆,并且調(diào)用了父類的dealloc(ARC環(huán)境下)
        if (cxx) object_cxxDestruct(obj); 
// 調(diào)用_object_remove_assocations函數(shù)清除對(duì)象的所有關(guān)聯(lián)對(duì)象勺馆。
        if (assoc) _object_remove_assocations(obj);
// 調(diào)用obj->clearDeallocating()函數(shù)清除所有的weak指針被將其置為nil。
        obj->clearDeallocating();
    }

    return obj;
}
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());
}
void 
objc_object::sidetable_clearDeallocating()
{
    SideTable& table = SideTables()[this];

    // clear any weak table items
    // clear extra retain count and deallocating bit
    // (fixme warn or abort if extra retain count == 0 ?)
    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();
}
NEVER_INLINE void
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();
}

從上面的調(diào)用順序可以看出

  1. dealloc調(diào)用_objc_rootDealloc調(diào)用objc_object::rootDealloc()
  2. objc_object::rootDealloc()調(diào)用object_dispose
  3. object_dispose調(diào)用objc_destructInstance
  4. objc_destructInstance調(diào)用obj->clearDeallocating()
  5. clearDeallocating里面有分支sidetable_clearDeallocating()和clearDeallocating_slow(),但是這2個(gè)函數(shù)里面其實(shí)都是調(diào)用了weak_clear_no_lock草穆。 所以我們來看看weak_clear_no_lock
核心方法weak_clear_no_lock
void 
weak_clear_no_lock(weak_table_t *weak_table, id referent_id) 
{
   //1灌灾、拿到被銷毀對(duì)象的指針
    objc_object *referent = (objc_object *)referent_id;
    //2、通過 指針 在weak_table中查找出對(duì)應(yīng)的entry
    weak_entry_t *entry = weak_entry_for_referent(weak_table, referent);
    if (entry == nil) {
        /// XXX shouldn't happen, but does with mismatched CF/objc
        //printf("XXX no entry for clear deallocating %p\n", referent);
        return;
    }
     //3悲柱、將所有的引用設(shè)置成nil
    // zero out references
    weak_referrer_t *referrers;
    size_t count;
    
    if (entry->out_of_line()) {
        //3.1锋喜、如果弱引用超過4個(gè)則將referrers數(shù)組內(nèi)的弱引用都置成nil。
        referrers = entry->referrers;
        count = TABLE_SIZE(entry);
    } 
    else {
       //3.2豌鸡、不超過4個(gè)則將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);
}

該函數(shù)的做了如下事情:

  1. 從weak表中獲取廢棄對(duì)象的地址為鍵值的記錄
  2. 將包含在記錄中的所有附有 weak修飾符變量的地址,賦值為nil
  3. 將weak表中該記錄刪除
  4. 從引用計(jì)數(shù)表中刪除廢棄對(duì)象的地址為鍵值的記錄

總結(jié)

嫌上面的內(nèi)容太多,那就看簡(jiǎn)化版
runtime維護(hù)了一個(gè)weak表涯冠,用于存儲(chǔ)指向某個(gè)對(duì)象的所有weak指針炉奴。weak表其實(shí)是一個(gè)hash(哈希)表,key是所指對(duì)象的地址蛇更,Value是weak指針的地址(這個(gè)地址的值是所指對(duì)象指針的地址)數(shù)組瞻赶。

稍微詳細(xì)版

  1. 初始化時(shí):runtime會(huì)調(diào)用objc_initWeak函數(shù),初始化一個(gè)新的weak指針指向?qū)ο蟮牡刂贰?/li>
  2. 添加引用時(shí):objc_initWeak函數(shù)會(huì)調(diào)用 storeWeak() 函數(shù)派任, storeWeak() 的作用是更新指針指向砸逊,創(chuàng)建對(duì)應(yīng)的弱引用表。
  3. 釋放時(shí),調(diào)用clearDeallocating函數(shù)吨瞎。clearDeallocating函數(shù)首先根據(jù)對(duì)象地址獲取所有weak指針地址的數(shù)組痹兜,然后遍歷這個(gè)數(shù)組把其中的數(shù)據(jù)設(shè)為nil,最后把這個(gè)entry從weak表中刪除,最后清理對(duì)象的記錄。
小尾巴

大家可能看了這篇文章還是暈乎乎的捶牢,主要是對(duì)里面的數(shù)據(jù)結(jié)構(gòu)不是很清楚,我又找到2篇很好的文章遗淳,看完這個(gè),保管完全不會(huì)暈乎乎了心傀。
iOS管理對(duì)象內(nèi)存的數(shù)據(jù)結(jié)構(gòu)以及操作算法--SideTables屈暗、RefcountMap、weak_table_t-一
iOS管理對(duì)象內(nèi)存的數(shù)據(jù)結(jié)構(gòu)以及操作算法--SideTables脂男、RefcountMap养叛、weak_table_t-二

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市宰翅,隨后出現(xiàn)的幾起案子弃甥,更是在濱河造成了極大的恐慌,老刑警劉巖汁讼,帶你破解...
    沈念sama閱讀 216,372評(píng)論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件淆攻,死亡現(xiàn)場(chǎng)離奇詭異阔墩,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)瓶珊,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門啸箫,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人伞芹,你說我怎么就攤上這事忘苛。” “怎么了唱较?”我有些...
    開封第一講書人閱讀 162,415評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵柑土,是天一觀的道長。 經(jīng)常有香客問我绊汹,道長,這世上最難降的妖魔是什么扮宠? 我笑而不...
    開封第一講書人閱讀 58,157評(píng)論 1 292
  • 正文 為了忘掉前任西乖,我火速辦了婚禮,結(jié)果婚禮上坛增,老公的妹妹穿的比我還像新娘获雕。我一直安慰自己,他們只是感情好收捣,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評(píng)論 6 388
  • 文/花漫 我一把揭開白布届案。 她就那樣靜靜地躺著,像睡著了一般罢艾。 火紅的嫁衣襯著肌膚如雪楣颠。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,125評(píng)論 1 297
  • 那天咐蚯,我揣著相機(jī)與錄音童漩,去河邊找鬼。 笑死春锋,一個(gè)胖子當(dāng)著我的面吹牛矫膨,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播期奔,決...
    沈念sama閱讀 40,028評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼侧馅,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了呐萌?” 一聲冷哼從身側(cè)響起馁痴,我...
    開封第一講書人閱讀 38,887評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎搁胆,沒想到半個(gè)月后弥搞,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體邮绿,經(jīng)...
    沈念sama閱讀 45,310評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評(píng)論 2 332
  • 正文 我和宋清朗相戀三年攀例,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了船逮。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,690評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡粤铭,死狀恐怖挖胃,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情梆惯,我是刑警寧澤酱鸭,帶...
    沈念sama閱讀 35,411評(píng)論 5 343
  • 正文 年R本政府宣布,位于F島的核電站垛吗,受9級(jí)特大地震影響凹髓,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜怯屉,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評(píng)論 3 325
  • 文/蒙蒙 一蔚舀、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧锨络,春花似錦赌躺、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至掠归,卻和暖如春缅叠,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背拂到。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評(píng)論 1 268
  • 我被黑心中介騙來泰國打工痪署, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人兄旬。 一個(gè)月前我還...
    沈念sama閱讀 47,693評(píng)論 2 368
  • 正文 我出身青樓狼犯,卻偏偏與公主長得像,于是被迫代替她去往敵國和親领铐。 傳聞我的和親對(duì)象是個(gè)殘疾皇子悯森,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容