Note that there are some explanatory texts on larger screens.

plurals
  1. POGNU GCC (g++): Why does it generate multiple dtors?
    primarykey
    data
    text
    <p>Developing environment: GNU GCC (g++) 4.1.2</p> <p>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?</p> <p>I tried and observed what I mentioned the above by using the following code.</p> <p>In "test.h"</p> <pre><code>class BaseClass { public: ~BaseClass(); void someMethod(); }; class DerivedClass : public BaseClass { public: virtual ~DerivedClass(); virtual void someMethod(); }; </code></pre> <p>In "test.cpp"</p> <pre><code>#include &lt;iostream&gt; #include "test.h" BaseClass::~BaseClass() { std::cout &lt;&lt; "BaseClass dtor invoked" &lt;&lt; std::endl; } void BaseClass::someMethod() { std::cout &lt;&lt; "Base class method" &lt;&lt; std::endl; } DerivedClass::~DerivedClass() { std::cout &lt;&lt; "DerivedClass dtor invoked" &lt;&lt; std::endl; } void DerivedClass::someMethod() { std::cout &lt;&lt; "Derived class method" &lt;&lt; std::endl; } int main() { BaseClass* b_ptr = new BaseClass; b_ptr-&gt;someMethod(); delete b_ptr; } </code></pre> <p>When I built the above code (g++ test.cpp -o test) and then see what kind of symbols have been generated as follows,</p> <p>nm --demangle test</p> <p>I could see the following output.</p> <pre><code>==== 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() </code></pre> <p>My questions are as follows.</p> <p>1) Why multiple dtors have been generated (BaseClass - 2, DerivedClass - 3)?</p> <p>2) What are the difference among these dtors? How those multiple dtors will be selectively used?</p> <p>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.</p> <p>I would greately appreciate if someone could give me the reply on the above.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload