美文网首页面试宝点
底层 - dealloc 的底层实现

底层 - dealloc 的底层实现

作者: 为自己丶拼个未来 | 来源:发表于2020-05-07 15:53 被阅读0次

官方给出的dealloc

截图

翻译

外部在什么情况下会触发dealloc方法?

  • 当一个对象retain count为0 (不再有强引用指向)时会触发dealloc。

注意直接把nil赋值给一个对象,不代表一定会触发dealloc。

dealloc 内部实现方法的调用时机又是 什么?

  1. 如果开发者实现了dealloc方法,那么先执行dealloc方法中的代码,执行完毕后在执行内部实现。

  2. 开发者并没有实现dealloc方法的情况下,直接执行系统内部方法调用。

dealloc 方法的作用是什么 ?为什么会暴露出来给开发者 ?

  • dealloc的作用就是释放当前接收器占用的内存空间。

  • 暴露出来是提供给开发者重写的 ,用来释放编码过程中用到的通知、Observer 等需要手动管理释放的操作方法。

dealloc 方法内部都做了些什么呢 ?

dealloc流程图

来自于 Apple 官方开源代码 objc4 -779 的源码分析

一 、dealloc 调用流程

1.首先调用 _objc_rootDealloc()

第一步

2.接下来调用 rootDealloc()

屏幕快照 2020-05-07 上午11.53.33.png
  1. isTaggedPointer 是否是标记指针 是直接 return ;

  2. 接下来会判断是否可以被直接快速释放,判断的依据主要有 5 个,判断是否有以下五种情况

  1. nonpointer 是否优化过isa指针

  2. weakly_reference 是否存在弱引用指向

  3. has_assoc 是否设置过关联对象

  4. has_cxx_dtor 是否有cpp的析构函数(.cxx_destruct)

  5. has_sidetable_rc 引用计数器是否过大无法存储在isa中

  1. 如果有以上五种任意一种,将会调用 object_dispose()方法,做下一步的处理。

  2. 如果没有之前五种情况的任意一种,则可以执行释放操作,C 函数的 free()。

  3. 执行完毕。

步骤3到5

二、object_dispose() 调用流程。

  1. 直接调用 objc_destructInstance()。

  2. 之后调用C的 free() 函数。

_object_dispose
  1. objc_destructInstance() 调用流程

    1. 先判断 hasCxxDtor,是否有析构函数(析构器),要调用 object_cxxDestruct() ,释放(清除成员变量)。

    2. 再判断hasAssocitatedObjects,如果有的话,要调用object_remove_associations(), 移除当前对象的关联对象。

    3. 然后调用 objc_clear_deallocating()。

    4. 执行完毕。


      屏幕快照 2020-05-07 下午12.06.20.png
  2. objc_clear_deallocating() 调用流程。

    1. 调用clearDeallocating()


      屏幕快照 2020-05-07 下午12.09.53.png
    2. 判断isa是否优化过,从arm64架构开始,对isa进行了优化,变成了一个共用体(union)结构,所以结果一般是优化过了。

    3. 判断是否有弱引用或者引用计数

    4. 执行 clearDeallocating_slow()


      步骤2到4
    5. 再执行 weak_clear_no_lock,在这一步骤中,会将指向该对象的弱引用指针置为nil。

    6. 接下来执行 table.refcnts.eraser(),从引用计数表中擦除该对象的引用计数。


      屏幕快照 2020-05-07 下午12.20.30.png
    7. .至此为止,Dealloc 的执行流程结束。

三、weak_clear_no_lock调用流程

  1. 去掉所有指向的弱指针
  2. 调用weak_entry_remove:从区域的弱引用表中删除条目。
/** 
 * Called by dealloc; nils out all weak pointers that point to the 
 * provided object so that they can no longer be used.
 * 
 * @param weak_table 
 * @param referent The object being deallocated. 
 */
void 
weak_clear_no_lock(weak_table_t *weak_table, id referent_id) 
{
    objc_object *referent = (objc_object *)referent_id;

    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;
    
    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;
            }
            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();
            }
        }
    }
    
    weak_entry_remove(weak_table, entry);
}

那么开发者重写dealloc后可以做什么,不可以做什么呢 ?

可以做什么 :

从 NotificationCenter remove observer、remove KVO, 有些非ARC管理的比如CFRelease , 有些需要流关闭,用了RAC持有disposable对象的可以dispose,这些都是比较常见的。还有一些会 removeGestureRecognizer, addToRunLoop 的removeFromRunLoop, dispatch_release掉自己持有的queue, 不过这些可能不是必要。

不可以做:

1 . 不要调用[super dealloc],实际上想调用也调用不了。。。 ARC编译器会自动调用,写了会直接报错程序无法运行;

  1. 不要使用点语法,建议使用_property 进行存取。因为点语法会触发setter 、getter方法,如果这些方法被重写,实现了一些不知道的操作,可能会涉及或者触发一些不应该在dealloc阶段做的事情比如对属性做了KVO监听。
因为这个方法暴露出来的目的就是为了让开发者来处理对象实例变量以外的资源。

相关文章

网友评论

    本文标题:底层 - dealloc 的底层实现

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