weak 原理

作者: 只写Bug程序猿 | 来源:发表于2020-03-03 11:58 被阅读0次

    weak作用

    weak我们常用来解决循环引用,因为weak修饰的对象引用计数不会增加,当对象释放的时候回自动置为nil,不会造成野指针.

    weak原理

    在 main函数里打上断点,然后看汇编

    20行打断点
    __weak汇编代码
    发现会调用objc_initWeak
    id
    objc_initWeak(id *location, id newObj)
    {
        if (!newObj) {
            *location = nil;
            return nil;
        }
    
        return storeWeak<DontHaveOld, DoHaveNew, DoCrashIfDeallocating>
            (location, (objc_object*)newObj);
    }
    

    然后调用storeWeak

    static id 
    storeWeak(id *location, objc_object *newObj)
    {
        assert(haveOld  ||  haveNew);
        if (!haveNew) assert(newObj == nil);
    
        Class previouslyInitializedClass = nil;
        id oldObj;
        //声明两个散列表
        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.
     retry:
        if (haveOld) {
            //通过oldObj获取oldtable
            oldObj = *location;
            oldTable = &SideTables()[oldObj];
        } else {
            oldTable = nil;
        }
        if (haveNew) {
            //通过newObj获取newTable
            newTable = &SideTables()[newObj];
        } else {
            newTable = nil;
        }
    
        SideTable::lockTwo<haveOld, haveNew>(oldTable, newTable);
    
        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.
        if (haveNew  &&  newObj) {
            Class cls = newObj->getIsa();
            if (cls != previouslyInitializedClass  &&  
                !((objc_class *)cls)->isInitialized()) 
            {
                SideTable::unlockTwo<haveOld, haveNew>(oldTable, newTable);
                class_initialize(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.
                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 returns nil if weak store should be rejected
    
            // Set is-weakly-referenced bit in refcount table.
            if (newObj  &&  !newObj->isTaggedPointer()) {
                newObj->setWeaklyReferenced_nolock();
            }
    
            // Do not set *location anywhere else. That would introduce a race.
            *location = (id)newObj;
        }
        else {
            // No new value. The storage is not changed.
        }
        
        SideTable::unlockTwo<haveOld, haveNew>(oldTable, newTable);
    
        return (id)newObj;
    }
    
    • 系统有一个大的散列表,散列表中有引用计数表,弱引用表,等小表
    • 如果weak指针有引用别的对象,调用weak_unregister_no_lock,通过objc找到entry数组,然后从数组中删除
    • 如果weak指针没有引用别的对象,调用weak_register_no_lock存储新值
    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;
      //通过table中弱引用对象referent  获取实体entry
    //如果存在entry就直接插入
        if ((entry = weak_entry_for_referent(weak_table, referent))) {
            append_referrer(entry, referrer);
        } 
        else {
            // 如果没有就直接创建了这个数组 - 插入weak_table
            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;
    }
    * 首先判断对象是否正在析构,正在析构直接跑出异常,正在析构的对象不能被弱引用
    * 通过弱引用对象获取weak_table中的实体entry
    * 如果存在entry直接将对象插入到弱引用表中
    * 如果不存在entry就新建一个entry数组,然后判断是否需要扩容,然后将对象加入到弱引用表中
    
    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;
        //获取实体entry
        if ((entry = weak_entry_for_referent(weak_table, referent))) {
            //移除referrer在weak_entry_t 的hash数组entry中的引用,并将数组元素置空
           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) {
                //将entry从weaktable中移除
                weak_entry_remove(weak_table, entry);
            }
        }
    
        // Do not set *referrer = nil. objc_storeWeak() requires that the 
        // value not change.
    }
    

    至此弱引用流程完毕
    那么他是怎么释放的呢,什么时候释放呢

    weak弱引用对象的释放
    - (void)dealloc {
        _objc_rootDealloc(self);
    }
    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.
            if (cxx) object_cxxDestruct(obj);
            //如果有关联对象则删除关联对象
            if (assoc) _object_remove_assocations(obj);
              //继续清理其他相关引用
            obj->clearDeallocating();
        }
    
        return obj;
    }
    
    

    dealloc最终会调用到一下代码

    void 
    weak_clear_no_lock(weak_table_t *weak_table, id referent_id) 
    {
           //获取弱引用对象地址
       objc_object *referent = (objc_object *)referent_id;
     //通过referent找到table中的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;
       }
    
       // zero out references
       weak_referrer_t *referrers;
       size_t count;
       //找到弱引用对象的所有weak指针数组
       if (entry->out_of_line()) {
           referrers = entry->referrers;
           count = TABLE_SIZE(entry);
       } 
       else {
           referrers = entry->inline_referrers;
           count = WEAK_INLINE_COUNT;
       }
       //遍历
       for (size_t i = 0; i < count; ++i) {
           objc_object **referrer = referrers[i];
           if (referrer) {
               if (*referrer == referent) {
                   //将referrer指针置为nil
                   *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();
               }
           }
       }
       将entry从weaktable中移除
       weak_entry_remove(weak_table, entry);
    }
    

    总结

    • 一个对象被弱引用之后,会将对象作为键,存储在sideTable中的weaktable的表中的对应的entry的数组中
    • 然后通过SideTable找到weak_table
    • 然后append_referrer(entry,referrer)将我的新弱引用的对象加到entry中去
    • 最后weak_entry_insert把entry加入到我们的weak_table中
    • deallc方法被调用时,会找到weaktable中对应的entry并且将数组元素置为nil,然后将entry充弱引用表中移除

    相关文章

      网友评论

        本文标题:weak 原理

        本文链接:https://www.haomeiwen.com/subject/ocfqlhtx.html