GNU GCC (g++):为什么它会生成多个 dtor?

2022-01-23 00:00:00 destructor g++ c++

开发环境: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"中

In "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 测试

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) 为什么生成了多个 dtor (BaseClass - 2, DerivedClass - 3)?

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

2) 这些 dtor 之间有什么区别?如何选择性地使用这些多个 dtor?

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

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

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.

推荐答案

首先,这些函数的用途在安腾 C++ ABI;请参阅基础对象析构函数"、完整对象析构函数"和删除析构函数"下的定义.5.1.4 中给出了到重整名称的映射.

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.

相关文章