天天看点

ARC下dealloc过程及.cxx_destruct的探究ARC下dealloc过程及.cxx_destruct的探究

这次探索源自于自己一直以来对<code>ARC</code>的一个疑问,在<code>MRC</code>时代,经常写下面的代码: 

对象析构时将内部其他对象<code>release</code>掉,申请的非Objc对象的内存当然也一并处理掉,最后调用<code>super</code>,继续将父类对象做析构。而现如今到了<code>ARC</code>时代,只剩下了下面的代码:

问题来了:

这个对象实例变量(Ivars)的释放去哪儿了? 

没有显示的调用<code>[super dealloc]</code>,上层的析构去哪儿了? 

<a target="_blank"></a>

A class may provide a method definition for an instance method named dealloc. This method will be called after the final release of the object but before it is deallocated or any of its instance variables are destroyed. The superclass’s implementation of dealloc will be called automatically when the method returns.

大概意思是:dealloc方法在最后一次release后被调用,但此时实例变量(Ivars)并未释放,父类的dealloc的方法将在子类dealloc方法返回后自动调用

The instance variables for an ARC-compiled class will be destroyed at some point after control enters the dealloc method for the root class of the class. The ordering of the destruction of instance variables is unspecified, both within a single class and between subclasses and superclasses.

理解:ARC下对象的实例变量在根类[NSObject dealloc]中释放(通常root class都是NSObject),变量释放顺序各种不确定(一个类内的不确定,子类和父类间也不确定,也就是说不用care释放顺序)

所以,不用主调<code>[super dealloc]</code>是因为自动调了,后面再说如何实现的;ARC下实例变量在根类NSObject析构时析构,下面就探究下。

通过apple的runtime源码,不难发现NSObject执行<code>dealloc</code>时调用<code>_objc_rootDealloc</code>继而调用<code>object_dispose</code>随后调用<code>objc_destructInstance</code>方法,前几步都是条件判断和简单的跳转,最后的这个函数如下:

简单明确的干了三件事:

执行一个叫<code>object_cxxDestruct</code>的东西干了点什么事

执行<code>_object_remove_assocations</code>去除和这个对象assocate的对象(常用于category中添加带变量的属性,这也是为什么ARC下没必要remove一遍的原因 (Edit: 在ARC或MRC下都不需要remove,感谢@sagles的基情提示)

执行<code>objc_clear_deallocating</code>,清空引用计数表并清除弱引用表,将所有<code>weak</code>引用指nil(这也就是weak变量能安全置空的所在)

所以,所探寻的ARC自动释放实例变量的地方就在<code>cxxDestruct</code>这个东西里面没跑了。

上面找到的名为<code>object_cxxDestruct</code>的方法最终成为下面的调用:

代码也不难理解,沿着继承链逐层向上搜寻<code>SEL_cxx_destruct</code>这个selector,找到函数实现(<code>void (*)(id)</code>(函数指针)并执行。

搜索这个selector的声明,发现是名为<code>.cxx_destruct</code>的方法,以点开头的名字,我想和unix的文件一样,是有隐藏属性的

ARC actually creates a -.cxx_destruct method to handle freeing instance variables. This method was originally created for calling C++ destructors automatically when an object was destroyed. 

和《Effective Objective-C 2.0》中提到的:

When the compiler saw that an object contained C++ objects, it would generate a method called .cxx_destruct. ARC piggybacks on this method and emits the required cleanup code within it. 

可以了解到,<code>.cxx_destruct</code>方法原本是为了C++对象析构的,ARC借用了这个方法插入代码实现了自动内存释放的工作

最好的办法还是写个测试代码把这个隐藏的方法找出来,其实在runtime中运行已经没什么隐藏可言了,简单的类结构如下:

只有两个简单的属性,找个地方写简单的测试代码:

主要目的是为了让这个对象走dealloc方法,新建的son对象过了大括号作用域就会释放了,所以在<code>after new</code>这行son对象初始化完成,在<code>gone</code>这行son对象被dealloc 

将这个扩展引入工程内,在<code>after new</code>处设置一个断点,run,trigger后使用lldb命令用这个扩展输出Son类所有的方法名:

ARC下dealloc过程及.cxx_destruct的探究ARC下dealloc过程及.cxx_destruct的探究

发现了这个<code>.cxx_destruct</code>方法,经过几次试验,发现:

只有在ARC下这个方法才会出现(试验代码的情况下)

只有当前类拥有实例变量时(不论是不是用property)这个方法才会出现,且父类的实例变量不会导致子类拥有这个方法

出现这个方法和变量是否被赋值,赋值成什么没有关系

依然在<code>after new</code>断点处,输入lldb命令:

将<code>name</code>的变量加入watchpoint,当这个变量被修改时会触发trigger:

ARC下dealloc过程及.cxx_destruct的探究ARC下dealloc过程及.cxx_destruct的探究

从中可以看出,在这个时刻,<code>_name</code>从0x00006b98变成了0x0,也就是nil,赶紧看下调用栈:

ARC下dealloc过程及.cxx_destruct的探究ARC下dealloc过程及.cxx_destruct的探究

发现果然跟到了<code>.cxx_destruct</code>方法,而且是在<code>objc_storeStrong</code>的过程中释放

——-

## 刨根问底.cxx_destruct

知道了ARC下对象实例变量的释放过程在<code>.cxx_destruct</code>内完成,但这个函数内部发生了什么,是如何调用<code>objc_storeStrong</code>释放变量的呢?

从上面的探究中知道,<code>.cxx_destruct</code>是编译器生成的代码,那它很可能在clang前端编译时完成,这让我联想到clang的<code>Code Generation</code>,因为之前曾经使用<code>clang -rewrite-objc xxx.m</code>时查看过官方文档留下了些印象,于是google:

结果发现clang的<code>doxygen</code>文档中<code>CodeGenModule</code>模块正是这部分的实现代码,cxx相关的代码生成部分源码在

<a target="_blank" href="http://clang.llvm.org/doxygen/CodeGenModule_8cpp-source.html">http://clang.llvm.org/doxygen/CodeGenModule_8cpp-source.html</a>

位于1827行,删减掉离题部分如下: 

这个函数大概作用是:获取<code>.cxx_destruct</code>的selector,创建Method,并加入到这个Class的方法列表中,最后一行的调用才是真的创建这个方法的实现。这个方法位于

<a target="_blank" href="http://clang.llvm.org/doxygen/CGObjC_8cpp_source.html">http://clang.llvm.org/doxygen/CGObjC_8cpp_source.html</a>

1354行,包含了构造和析构的cxx方法,继续跟随<code>.cxx_destruct</code>,最终调用<code>emitCXXDestructMethod</code>函数,代码如下: 

分析这段代码以及其中调用后发现:它遍历当前对象所有的实例变量(Ivars),调用<code>objc_storeStrong</code>,从<code>clang</code>的ARC文档上可以找到<code>objc_storeStrong</code>的示意代码实现如下:

在<code>.cxx_destruct</code>进行形如<code>objc_storeStrong(&amp;ivar, null)</code>的调用后,这个实例变量就被<code>release</code>和设置成<code>nil</code>了

按照上面的思路,自动调用<code>[super dealloc]</code>也一定是<code>CodeGen</code>干的工作了

<code>StartObjCMethod</code>方法中: 

上面代码可以得知在调用<code>dealloc</code>方法时被插入了代码,由<code>FinishARCDealloc</code>结构定义: 

上面代码基本上就是向父类转发<code>dealloc</code>的调用,实现了自动调用<code>[super dealloc]</code>方法。 

ARC下对象的成员变量于编译器插入的<code>.cxx_desctruct</code>方法自动释放

ARC下<code>[super dealloc]</code>方法也由编译器自动插入

所谓<code>编译器插入代码</code>过程需要进一步了解,还不清楚其运作方式

clang的<code>CodeGen</code>也值得深入研究一下

<a target="_blank" href="http://clang.llvm.org/docs/AutomaticReferenceCounting.html">http://clang.llvm.org/docs/AutomaticReferenceCounting.html</a>

<a target="_blank" href="http://my.safaribooksonline.com/book/programming/objective-c/9780132908641/3dot-memory-management/ch03">http://my.safaribooksonline.com/book/programming/objective-c/9780132908641/3dot-memory-management/ch03</a>

ARC下dealloc过程及.cxx_destruct的探究ARC下dealloc过程及.cxx_destruct的探究
ARC下dealloc过程及.cxx_destruct的探究ARC下dealloc过程及.cxx_destruct的探究
ARC下dealloc过程及.cxx_destruct的探究ARC下dealloc过程及.cxx_destruct的探究

继续阅读