I'm using C++11 and g++4.8 on Ubuntu Trusty.
Consider this snippet
class Parent {
public:
virtual ~Parent() = default;
virtual void f() = 0;
};
class Child: public Parent {
public:
void f(){}
};
Called using
{
Child o;
o.f();
}
{
Parent * o = new Child;
delete o;
}
{
Child * o = new Child;
delete o;
}
I use gcov to generate my code coverage report. It report that the destructor with symbol _ZN6ParentD0Ev
is never called, while _ZN6ParentD2Ev
is.
Answer Dual emission of constructor symbols and GNU GCC (g++): Why does it generate multiple dtors? reports that _ZN6ParentD0Ev
is the deleting constructor.
Is there any case where this "deleting destructor" is called on the Parent
class ?
Subsidiary question: if not, is there a way to get the gcov/lcov code coverage tool (used following answer of Detailed guide on using gcov with CMake/CDash?) ignore that symbol in its report ?
No, destructors are called automatically in the reverse order of construction. (Base classes last). Do not call base class destructors. you can't have a pure virtual destructor without a body.
While defining (providing an implementation) pure virtual methods is rarely useful, you must define a pure virtual destructor. This is because the destructor of a base class is always called when a derived object is destroyed. Failing to define it will cause a link error.
The vector deleting destructor takes some flags. If 2 is set, then a vector is being destructed; otherwise a single object is being destructed. If 1 is set, then the memory is also freed. In our case, the flags parameter is 3, so we will perform a vector destruct followed by a delete.
Yes, it is possible to have a pure virtual destructor. Pure virtual destructors are legal in standard C++ and one of the most important things to remember is that if a class contains a pure virtual destructor, it must provide a function body for the pure virtual destructor.
You can't have Parent objects, so no. It's a GCC oversight that this needless function is generated. The optimizer really should remove it, as it's unused, but I've found that GCC has problems is that area as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With