• dealloc实现原理


    当对象引用计数为0时,运行时会调用_objc_rootDealloc,实现如下:

    - (void)dealloc {
        _objc_rootDealloc(self);
    }
    _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);
        }
    }

    isTaggedPointer 小标记指针直接返回,不用释放
    对象采用了优化的isa计数方式(isa.nonpointer)
    对象没有被weak引用!isa.weakly_referenced
    没有关联对象!isa.has_assoc
    没有自定义的C++析构方法!isa.has_cxx_dtor
    没有用到sideTable来做引用计数 !isa.has_sidetable_rc

    如果满足以上条件 则可直接调用free释放内存,否则会调用object_dispose函数

    id 
    object_dispose(id obj)
    {
        if (!obj) return nil;
        // 析构obj
        objc_destructInstance(obj);
        // 释放内存
        free(obj);
    
        return nil;
    }
    void *objc_destructInstance(id obj) 
    {
        if (obj) {
            // Read all of the flags at once for performance.
            //c++析构函数
            bool cxx = obj->hasCxxDtor();
            //关联对象
            bool assoc = obj->hasAssociatedObjects();
    
            // 如果有c++析构函数 则调用c++析构函数.
            if (cxx)
                object_cxxDestruct(obj);
    
            // 如果有关联对象则移除关联对象
            if (assoc)
                _object_remove_assocations(obj);
            // 清理引用计数以及弱引用
            obj->clearDeallocating();
        }
    
        return obj;
    }

    至此对象相关的cxx虚函数、关联对象、弱引用全部释放完毕

     
  • 相关阅读:
    NSURLSession的文件下载
    JSON解析(序列化和反序列化)
    NSURLSession的知识小记
    RunLoop的知识小记
    NSCach 的知识小记
    多图片下载综合案例-磁盘缓存处理
    模仿UIApplication创建单例
    LayoutSubviews的调用
    setValueForKeysWithDictionary的底层实现
    剑指offer 20:顺时针打印矩阵
  • 原文地址:https://www.cnblogs.com/huangzs/p/16241845.html
Copyright © 2020-2023  润新知