My question is building on this question: Correct way to inherit from a virtual class with non-virtual parent.
Is my understanding right that in the case which is described in the question, the Three and Two part of new allocated object are leaking because they are not destructed?
Source:
#include <iostream>
struct One
{
~One() {
std::cout << "~One()\n";
}
};
struct Two : One
{
virtual ~Two() {
std::cout << "~Two()\n";
}
virtual void test() = 0;
};
struct Three : Two
{
virtual ~Three() {
std::cout << "~Three()\n";
}
virtual void test() {
std::cout << "Three::test()\n";
}
};
int main()
{
Two* two = new Three;
two->test();
One* one = two;
delete one;
}
Yes, that's correct. The definition of a memory leak is a situation where you are unable to delete something that you created (and whose lifetime you are therefore responsible for managing).
As the answers to that question indicate,
delete one
invokes undefined behavior (which in most cases will probably translate to a regular old memory leak, but things could be as bad as nasal demons) because the runtime type of the specified object does not match its static (declared) type, and the static type does not have a virtual destructor.The applicable section from the C++ standard is this one:
The solution is either to declare all of the destructors
virtual
, or not to delete the objects through a pointer toOne
.