Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GNU GCC (g++): Why does it generate multiple dtors?

Developing environment: GNU GCC (g++) 4.1.2

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.

In "test.h"

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

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; } 

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

I could see the following output.

==== 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() 

My questions are as follows.

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

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

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.

like image 742
Smg Avatar asked Jul 07 '11 16:07

Smg


1 Answers

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.

Basically:

  • 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.

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.

like image 199
bdonlan Avatar answered Oct 05 '22 22:10

bdonlan