zoukankan      html  css  js  c++  java
  • Objective-C 对象释放方法调用过程

    1.objc_object::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  &&          // 无 C++ 析构函数
                     !isa.has_sidetable_rc))        // 未使用 SideTable
        {
            assert(!sidetable_present());
            free(this);
        } 
        else {
            object_dispose((id)this);
        }
    }
    

    2.object_dispose

    
    /***********************************************************************
    * object_dispose
    * fixme
    * Locking: none
    **********************************************************************/
    id 
    object_dispose(id obj)
    {
        if (!obj) return nil;
    
        objc_destructInstance(obj);    
        free(obj);
    
        return nil;
    }
    
    

    3.objc_destructInstance

    
    /***********************************************************************
    * 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();               // 类及父类是否有自己的析构函数
            bool assoc = obj->hasAssociatedObjects();   // 当前对象是否有关联对象
    
            // This order is important.
            if (cxx) object_cxxDestruct(obj);
            if (assoc) _object_remove_assocations(obj);
            obj->clearDeallocating();
        }
    
        return obj;
    }
    
    

    4.objc_object::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());
    }
    
    

    5.objc_object::clearDeallocating_slow

    // Slow path of clearDeallocating() 
    // for objects with nonpointer isa
    // that were ever weakly referenced 
    // or whose retain count ever overflowed to the side table.
    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);   // 清空 DenseMap 中的引用计数数据
        }
        table.unlock();
    }
    
    
    // 未使用指针优化调用
    
    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();
    }
    
     
    1人点赞
     
    iOS
     


    作者:MangK
    链接:https://www.jianshu.com/p/0b77593e10aa
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
    ------------------越是喧嚣的世界,越需要宁静的思考------------------ 合抱之木,生于毫末;九层之台,起于垒土;千里之行,始于足下。 积土成山,风雨兴焉;积水成渊,蛟龙生焉;积善成德,而神明自得,圣心备焉。故不积跬步,无以至千里;不积小流,无以成江海。骐骥一跃,不能十步;驽马十驾,功在不舍。锲而舍之,朽木不折;锲而不舍,金石可镂。蚓无爪牙之利,筋骨之强,上食埃土,下饮黄泉,用心一也。蟹六跪而二螯,非蛇鳝之穴无可寄托者,用心躁也。
  • 相关阅读:
    【php-04控制流程】
    【php-03函数】
    【php-02-变量】
    【php-01-标记符】
    【CSS 基础面试题】
    【CSS3特效之转化(transform)和过渡(transition)】
    【jsonp】
    【PHP 面试知识梳理】
    OM模块功能&API详解
    EBS 多SHEET页EXCEL动态报表开发过程
  • 原文地址:https://www.cnblogs.com/feng9exe/p/14533958.html
Copyright © 2011-2022 走看看