iOS Dealloc 在内存管理中的实现机制

1 介绍

在 Objective-C 中,dealloc 方法是用来释放对象资源的。当一个对象被释放时(即引用计数变为零),系统会自动调用该对象的 dealloc 方法。在 dealloc 方法中,可以执行一些清理操作,比如释放对象持有的资源、取消观察者或通知、执行其他的清理操作等。

在 Swift 中,使用 deinit 方法来执行类似的操作。当对象的引用计数降为零时,系统会自动调用该对象的 deinit 方法来执行清理操作。

2 实现机制

2.1 Dealloc 调用流程

  1. 首先调用 _objc_rootDealloc() 方法。
scss 复制代码
- (void)dealloc {
    _objc_rootDealloc(self);
}
  1. 接着调用 rootDealloc() 方法。
arduino 复制代码
objc_object::rootDealloc()
{
    if (isTaggedPointer()) return;

    if (fastpath(isa.nonpointer                     &&
                 !isa.weakly_referenced             &&
                 !isa.has_assoc                     &&
#if ISA_HAS_CXX_DTOR_BIT
                 !isa.has_cxx_dtor                  &&
#else
                 !isa.getClass(false)->hasCxxDtor() &&
#endif
                 !isa.has_sidetable_rc))
    {
        assert(!sidetable_present());
        free(this);
    } 
    else {
        object_dispose((id)this);
    }
}
  1. 在这一步,会判断对象是否可以被释放,主要根据以下五种情况进行判断:
  • NONPointer_ISA:是否为一个固定的非指针值
  • weakly_reference:是否有弱引用
  • has_assoc:是否有关联对象
  • has_cxx_dtor:是否有 C++ 析构函数
  • has_sidetable_rc:是否使用 sidetable 进行引用计数管理

sidetable 是指 Objective-C 中用于管理一些特殊情况下的引用计数的数据结构。在对象的引用计数比较大或者需要支持多线程时,Objective-C 运行时会采用 sidetable 来进行引用计数的管理。sidetable 是一种哈希表的形式,用于存储额外的引用计数,这样可以更高效地管理对象的引用计数。在释放对象时,需要清理 sidetable 中的相关信息,以确保内存正确释放。

  1. 流程处流
  • 如果满足以上任意一种情况,将会调用 object_dispose() 方法进行下一步处理。
  • 如果不满足上述五种情况,则执行释放操作,即调用 C 函数的 free()
  1. 执行完毕。

2.2 object_dispose() 调用流程

scss 复制代码
object_dispose(id obj)
{
    if (!obj) return nil;

    objc_destructInstance(obj);    
    free(obj);

    return nil;
}
  1. 直接调用 objc_destructInstance() 方法。
  2. 然后调用 C 函数 free() 进行内存释放。

2.3 objc_destructInstance() 调用流程

scss 复制代码
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, /*deallocating*/true);
        obj->clearDeallocating();
    }

    return obj;
}
  1. 首先判断是否有 C++ 相关内容(hasCxxDtor),如果有,则调用 object_cxxDestruct() 方法销毁 C++ 相关内容。
  2. 接着判断是否有关联对象(hasAssociatedObjects),如果有,则调用 object_remove_associations() 方法销毁关联对象。
  3. 调用 clearDeallocating() 方法。
  4. 执行完毕。

2.4 clearDeallocating() 调用流程

scss 复制代码
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());
}
  1. 先执行 sidetable_clearDeallocating() 方法。
  2. 然后执行 weak_clear_no_lock 方法,在这一步骤中,将指向该对象的弱引用指针置为 nil。
  3. 接着执行 table.refcnts.erase() 方法,从引用计数表中擦除该对象的引用计数。
  4. 至此,Dealloc 的执行流程结束。

3 开发问题

ViewController 不执行 dealloc 方法的情况?

  1. 循环引用,例如 block、delegate
  2. 未正确释放资源,例如通知、观察者、NSTimer
  3. 其他
    • 第三方库或框架可能会影响 ViewController 的生命周期,导致 dealloc 方法不被调用。
    • WKWebView 适时移除消息处理程序 removeScriptMessageHandlerForName:
相关推荐
权咚8 小时前
阿权的开发经验小集
git·ios·xcode
用户098 小时前
TipKit与CloudKit同步完全指南
ios·swift
法的空间14 小时前
Flutter JsonToDart 支持 JsonSchema
android·flutter·ios
2501_9159184116 小时前
iOS 上架全流程指南 iOS 应用发布步骤、App Store 上架流程、uni-app 打包上传 ipa 与审核实战经验分享
android·ios·小程序·uni-app·cocoa·iphone·webview
00后程序员张17 小时前
iOS App 混淆与加固对比 源码混淆与ipa文件混淆的区别、iOS代码保护与应用安全场景最佳实践
android·安全·ios·小程序·uni-app·iphone·webview
Magnetic_h1 天前
【iOS】设计模式复习
笔记·学习·ios·设计模式·objective-c·cocoa
00后程序员张1 天前
详细解析苹果iOS应用上架到App Store的完整步骤与指南
android·ios·小程序·https·uni-app·iphone·webview
前端小超超1 天前
capacitor配置ios应用图标不同尺寸
ios·蓝桥杯·cocoa
2501_915106321 天前
Xcode 上传 ipa 全流程详解 App Store 上架流程、uni-app 生成 ipa 文件上传与审核指南
android·macos·ios·小程序·uni-app·iphone·xcode
kymjs张涛1 天前
零一开源|前沿技术周刊 #16
ios·apple·hacker news