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

前言

此次探索源自于本身一直以来对ARC的一个疑问,在MRC时代,常常写下面的代码:html

1
2
3
4
5
6
7
8
9
- (void)dealloc
{
     self.array = nil;
     self.string = nil;
     // ... //
     // 非Objc对象内存的释放,如CFRelease(...)
     // ... //
     [super dealloc];
}

对象析构时将内部其余对象release掉,申请的非Objc对象的内存固然也一并处理掉,最后调用super,继续将父类对象作析构。而现现在到了ARC时代,只剩下了下面的代码:前端

1
2
3
4
5
6
- (void)dealloc
{
     // ... //
     // 非Objc对象内存的释放,如CFRelease(...)
     // ... //
}

问题来了:git

  1. 这个对象实例变量(Ivars)的释放去哪儿了?
  2. 没有显示的调用[super dealloc],上层的析构去哪儿了?

ARC文档中对dealloc过程的解释

llvm官方的ARC文档中对ARC下的dealloc过程作了简单说明,从中仍是能找出些有用的信息:github

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.objective-c

  • 大概意思是: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释放顺序)

因此,不用主调[super dealloc]是由于自动调了,后面再说如何实现的;ARC下实例变量在根类NSObject析构时析构,下面就探究下。app

NSObject的析构过程

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void *objc_destructInstance(id obj)
{
     if (obj) {
         Class isa_gen = _object_getClass(obj);
         class_t *isa = newcls(isa_gen);
 
         // Read all of the flags at once for performance.
         bool cxx = hasCxxStructors(isa);
         bool assoc = !UseGC && _class_instancesHaveAssociatedObjects(isa_gen);
 
         // This order is important.
         if (cxx) object_cxxDestruct(obj);
         if (assoc) _object_remove_assocations(obj);
 
         if (!UseGC) objc_clear_deallocating(obj);
     }
 
     return obj;
}

简单明确的干了三件事:函数

  1. 执行一个叫object_cxxDestruct的东西干了点什么事
  2. 执行_object_remove_assocations去除和这个对象assocate的对象(经常使用于category中添加带变量的属性,这也是为何ARC下不必remove一遍的缘由)
  3. 执行objc_clear_deallocating,清空引用计数表并清除弱引用表,将全部weak引用指nil(这也就是weak变量能安全置空的所在)

因此,所探寻的ARC自动释放实例变量的地方就在cxxDestruct这个东西里面没跑了。工具

探寻隐藏的.cxx_destruct

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
static void object_cxxDestructFromClass(id obj, Class cls)
{
     void (*dtor)(id);
 
     // Call cls's dtor first, then superclasses's dtors.
 
     for ( ; cls != NULL; cls = _class_getSuperclass(cls)) {
         if (!_class_hasCxxStructors(cls)) return;
         dtor = (void(*)(id))
             lookupMethodInClassAndLoadCache(cls, SEL_cxx_destruct);
         if (dtor != (void(*)(id))_objc_msgForward_internal) {
             if (PrintCxxCtors) {
                 _objc_inform("CXX: calling C++ destructors for class %s",
                              _class_getName(cls));
             }
             (*dtor)(obj);
         }
     }
}

代码也不难理解,沿着继承链逐层向上搜寻SEL_cxx_destruct这个selector,找到函数实现(void (*)(id)(函数指针)并执行。
搜索这个selector的声明,发现是名为.cxx_destruct的方法,以点开头的名字,我想和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.

能够了解到,.cxx_destruct方法本来是为了C++对象析构的,ARC借用了这个方法插入代码实现了自动内存释放的工做

经过实验找出.cxx_destruct

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

1
2
3
4
5
6
7
@interface Father : NSObject
@property (nonatomic, copy) NSString *name;
@end
 
@interface Son : Father
@property (nonatomic, copy) NSArray *toys;
@end

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

1
2
3
4
5
6
7
8
9
// start
     {
         // before new
         Son *son = [Son new];
         son.name = @"sark";
         son.toys = @[@"sunny", @"xx"];
         // after new
     }
     // gone

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

我的一直喜欢使用NSObject+DLIntrospection这个扩展做为调试工具,能够轻松打出一个类的方法,变量等等。

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

rehe51530583gw1ef27srhw7lj208b05ujrq

发现了这个.cxx_destruct方法,通过几回试验,发现:

  1. 只有在ARC下这个方法才会出现(试验代码的状况下)
  2. 只有当前类拥有实例变量时(不管是不是用property)这个方法才会出现,且父类的实例变量不会致使子类拥有这个方法
  3. 出现这个方法和变量是否被赋值,赋值成什么没有关系

使用watchpoint定位内存释放时刻

依然在after new断点处,输入lldb命令:

1
watchpoint set variable son->_name

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

regreg51530583gw1ef28rn41lcj20fs03aq3b

从中能够看出,在这个时刻,_name从0x00006b98变成了0×0,也就是nil,赶忙看下调用栈:

erhrth51530583gw1ef2911o40zj20a605yweu

发现果真跟到了.cxx_destruct方法,并且是在objc_storeStrong的过程当中释放

刨根问底.cxx_destruct

知道了ARC下对象实例变量的释放过程在.cxx_destruct内完成,但这个函数内部发生了什么,是如何调用objc_storeStrong释放变量的呢?
从上面的探究中知道,.cxx_destruct是编译器生成的代码,那它极可能在clang前端编译时完成,这让我联想到clang的Code Generation,由于以前曾经使用clang -rewrite-objc xxx.m时查看过官方文档留下了些印象,因而google:

1
.cxx_destruct site:clang.llvm.org

结果发现clang的doxygen文档中CodeGenModule模块正是这部分的实现代码,cxx相关的代码生成部分源码在
http://clang.llvm.org/doxygen/CodeGenModule_8cpp-source.html
位于1827行,删减掉离题部分以下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/// EmitObjCIvarInitializations - Emit information for ivar initialization
/// for an implementation.
void CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D)
{
     DeclContext* DC = const_cast<DeclContext*>(dyn_cast<DeclContext>(D));
     assert(DC && "EmitObjCIvarInitializations - null DeclContext");
     IdentifierInfo *II = &getContext().Idents.get(".cxx_destruct");
     Selector cxxSelector = getContext().Selectors.getSelector(0, &II);
     ObjCMethodDecl *DTORMethod = ObjCMethodDecl::Create(getContext(),
                                                         D->getLocation(),
                                                         D->getLocation(), cxxSelector,
                                                         getContext().VoidTy, 0,
                                                         DC, true, false, true,
                                                         ObjCMethodDecl::Required);
    D->addInstanceMethod(DTORMethod);
    CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, DTORMethod, false);
}

这个函数大概做用是:获取.cxx_destruct的selector,建立Method,并加入到这个Class的方法列表中,最后一行的调用才是真的建立这个方法的实现。这个方法位于
http://clang.llvm.org/doxygen/CGObjC_8cpp_source.html
1354行,包含了构造和析构的cxx方法,继续跟随.cxx_destruct,最终调用emitCXXDestructMethod函数,代码以下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
static void emitCXXDestructMethod(CodeGenFunction &CGF, ObjCImplementationDecl *impl)
{
    CodeGenFunction::RunCleanupsScope scope(CGF);
 
    llvm::Value *self = CGF.LoadObjCSelf();
 
    const ObjCInterfaceDecl *iface = impl->getClassInterface();
    for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin(); ivar; ivar = ivar->getNextIvar())
    {
      QualType type = ivar->getType();
 
      // Check whether the ivar is a destructible type.
      QualType::DestructionKind dtorKind = type.isDestructedType();
      if (!dtorKind) continue;
 
      CodeGenFunction::Destroyer *destroyer = 0;
 
      // Use a call to objc_storeStrong to destroy strong ivars, for the
      // general benefit of the tools.
      if (dtorKind == QualType::DK_objc_strong_lifetime) {
        destroyer = destroyARCStrongWithStore;
 
      // Otherwise use the default for the destruction kind.
      } else {
        destroyer = CGF.getDestroyer(dtorKind);
      }
 
      CleanupKind cleanupKind = CGF.getCleanupKind(dtorKind);
      CGF.EHStack.pushCleanup<DestroyIvar>(cleanupKind, self, ivar, destroyer,
                                           cleanupKind & EHCleanup);
    }
 
    assert(scope.requiresCleanups() && "nothing to do in .cxx_destruct?");
}

分析这段代码以及其中调用后发现:它遍历当前对象全部的实例变量(Ivars),调用objc_storeStrong,从clang的ARC文档上能够找到objc_storeStrong的示意代码实现以下:

1
2
3
4
5
6
7
id objc_storeStrong(id *object, id value) {
   value = [value retain];
   id oldValue = *object;
   *object = value;
   [oldValue release];
   return value;
}

.cxx_destruct进行形如objc_storeStrong(&ivar, null)的调用后,这个实例变量就被release和设置成nil
注:真实的实现能够参考 http://clang.llvm.org/doxygen/CGObjC_8cpp_source.html 2078行

自动调用[super dealloc]的实现

按照上面的思路,自动调用[super dealloc]也必定是CodeGen干的工做了 位于 http://clang.llvm.org/doxygen/CGObjC_8cpp_source.html 492行
StartObjCMethod方法中:

1
2
if (ident->isStr("dealloc"))
     EHStack.pushCleanup<FinishARCDealloc>(getARCCleanupKind());

上面代码能够得知在调用dealloc方法时被插入了代码,由FinishARCDealloc结构定义:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
struct FinishARCDealloc : EHScopeStack::Cleanup {
    void Emit(CodeGenFunction &CGF, Flags flags) override {
      const ObjCMethodDecl *method = cast<ObjCMethodDecl>(CGF.CurCodeDecl);
 
      const ObjCImplDecl *impl = cast<ObjCImplDecl>(method->getDeclContext());
      const ObjCInterfaceDecl *iface = impl->getClassInterface();
      if (!iface->getSuperClass()) return;
 
      bool isCategory = isa<ObjCCategoryImplDecl>(impl);
 
      // Call [super dealloc] if we have a superclass.
      llvm::Value *self = CGF.LoadObjCSelf();
 
      CallArgList args;
      CGF.CGM.getObjCRuntime().GenerateMessageSendSuper(CGF, ReturnValueSlot(),
                                                        CGF.getContext().VoidTy,
                                                        method->getSelector(),
                                                        iface,
                                                        isCategory,
                                                        self,
                                                        /*is class msg*/ false,
                                                        args,
                                                        method);
    }
};

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

总结

    • ARC下对象的成员变量于编译器插入的.cxx_desctruct方法自动释放
    • ARC下[super dealloc]方法也由编译器自动插入
    • 所谓编译器插入代码过程须要进一步了解,还不清楚其运做方式
    • clang的CodeGen也值得深刻研究一下

 

原文连接:http://blog.jobbole.com/65028/

相关文章
相关标签/搜索