GNU GCC(g ++):为什么会生成多个dtors? [英] GNU GCC (g++): Why does it generate multiple dtors?

查看:257
本文介绍了GNU GCC(g ++):为什么会生成多个dtors?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开发环境:GNU GCC(g ++)4.1.2

Developing environment: GNU GCC (g++) 4.1.2

我试图研究如何增加单元测试中的代码覆盖率,我发现一些类dtor似乎被生成多次。

While I'm trying to investigate how to increase 'code coverage - particularly function coverage' in unit testing, I've found that some of class dtor seems to be generated multiple times. Does some of you have any idea on why, please?

我尝试通过使用以下代码观察我提到的上述内容。

I tried and observed what I mentioned the above by using the following code.

在test.h

class BaseClass
{
public:
    ~BaseClass();
    void someMethod();
};

class DerivedClass : public BaseClass
{
public:
    virtual ~DerivedClass();
    virtual void someMethod();
};

在test.cpp中

#include <iostream>
#include "test.h"

BaseClass::~BaseClass()
{
    std::cout << "BaseClass dtor invoked" << std::endl;
}

void BaseClass::someMethod()
{
    std::cout << "Base class method" << std::endl;
}

DerivedClass::~DerivedClass()
{
    std::cout << "DerivedClass dtor invoked" << std::endl;
}

void DerivedClass::someMethod()
{
    std::cout << "Derived class method" << std::endl;
}

int main()
{
    BaseClass* b_ptr = new BaseClass;
    b_ptr->someMethod();
    delete b_ptr;
}



当我构建上述代码(g ++ test.cpp -o test)然后查看已生成的符号类型如下:

When I built the above code (g++ test.cpp -o test) and then see what kind of symbols have been generated as follows,

nm --demangle test

nm --demangle test

以下输出。

==== following is partial output ====
08048816 T DerivedClass::someMethod()
08048922 T DerivedClass::~DerivedClass()
080489aa T DerivedClass::~DerivedClass()
08048a32 T DerivedClass::~DerivedClass()
08048842 T BaseClass::someMethod()
0804886e T BaseClass::~BaseClass()
080488f6 T BaseClass::~BaseClass()

我的问题如下。

1)为什么生成了多个dtors(BaseClass - 2,DerivedClass - 3)?

1) Why multiple dtors have been generated (BaseClass - 2, DerivedClass - 3)?

2)这些dtors有什么区别?

2) What are the difference among these dtors? How those multiple dtors will be selectively used?

我现在有一种感觉,为了实现C ++项目的100%功能覆盖,我们需要理解这一点我可以在我的单元测试中调用所有的dtors。

I now have a feeling that in order to achieve 100% function coverage for C++ project, we would need to understand this so that I can invoke all those dtors in my unit tests.

如果有人可以给我上面的答复,我会很感激。

I would greately appreciate if someone could give me the reply on the above.

推荐答案

首先,这些函数的用途在 Itanium C ++ ABI ;请参见基础对象析构函数,完整对象析构函数和删除析构函数中的定义。

First, the purposes of these functions are described in the Itanium C++ ABI; see definitions under "base object destructor", "complete object destructor", and "deleting destructor". The mapping to mangled names is given in 5.1.4.

基本上:


  • D2是基础对象析构函数。它会销毁对象本身,以及数据成员和非虚拟基类。

  • D1是完整对象析构函数。它还会销毁虚拟基类。

  • D0是删除对象析构函数。它执行一切完整的对象析构函数,加上它调用 operator delete 来实际释放内存。

  • D2 is the "base object destructor". It destroys the object itself, as well as data members and non-virtual base classes.
  • D1 is the "complete object destructor". It additionally destroys virtual base classes.
  • D0 is the "deleting object destructor". It does everything the complete object destructor does, plus it calls operator delete to actually free the memory.

如果没有虚拟基类,则D2和D1是相同的; GCC将在足够的优化级别上将符号实际上用于两者的相同代码。

If you have no virtual base classes, D2 and D1 are identical; GCC will, on sufficient optimization levels, actually alias the symbols to the same code for both.

这篇关于GNU GCC(g ++):为什么会生成多个dtors?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆