dealloc

turboksiOS263

当调用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;

}



相关文章

OC的实例对象、类对象、元类对象的关系

OC的实例对象、类对象、元类对象的关系

Objective-C中的对象、简称OC对象,主要分为三种:instance对象(实例对象)、class对象(类对象)、meta-class对象(元类对象)。一:instance对象instance对...

iOS一行代码搞定自定义AlertView

iOS一行代码搞定自定义AlertView

效果图走一走:使用方式: pod  'KSAlertView'举例使用,单按钮Alert: [[[KSAlertView alloc] initWithT...

iOS的block

iOS的block

一:block的本质1.    block本质上是一个OC对象,它内部有个isa指针2.    block是封装了函数调用以及函数调用环境的OC对象3.&nbs...

iOS程序的内存布局

iOS程序的内存布局

    代码段:编译之后的代码    数据段:字符串常量:比如NSString * str = @“123”已初始化数据:已初始化的全局变量...

iOS的分类(Category)

iOS的分类(Category)

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

iOS音视频之视频播放

iOS音视频之视频播放

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