dealloc

turboksiOS179

当调用dealloc方法的时候,执行顺序可以在源码中查找如下(objc源码):


1.

// Replaced by NSZombies

- (void)dealloc {

    _objc_rootDealloc(self);

}


2.    

void

_objc_rootDealloc(id obj)

{

    assert(obj);


    obj->rootDealloc();

}


3.    

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);

    }

}


4.    

id 

object_dispose(id obj)

{

    if (!obj) return nil;


    objc_destructInstance(obj);    

    free(obj);


    return nil;

}


5.

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();   //将指向当前对象的弱指针置为nil

    }


    return obj;

}



相关文章

Runtime动态创建类

Runtime动态创建类

- (void)viewDidLoad {    [super viewDidLoad];    Class KSObj = objc_allocateClas...

iOS的分类(Category)

iOS的分类(Category)

一:Category的底层结构定义在objc-runtime-new.h中:struct category_t {    const char *name;   ...

iOS音视频之视频播放

iOS音视频之视频播放

由于MPMoviePlayerController与MPMoviePlayerViewController在iOS9.0之后被弃用,所以采用AVPlayer以及AVPlayerViewControll...

Runtime详解

Runtime详解

The Objective-C language defers as many decisions as it can from compile time and link time to runti...

RunLoop

RunLoop

RunLoop一、基础理论顾名思义1.    运行循环2.    在程序运行过程中循环做一些事情3.    是用户态和内核态之间的交互模式应...

iOS 仿抖音打卡美好中国

iOS 仿抖音打卡美好中国

效果图这几天打卡中国挺火的,刚好有点时间就想着看看能不能模仿一下。Scrollview + CADisplayLink 设置背景UILongPressGestureRecognizer长按手...