Correct way to inherit from a virtual class with non-virtual parent continued

96 views Asked by At

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

There are 1 answers

0
Cody Gray - on strike On BEST ANSWER

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:

§5.3.5/3: In the first alternative (delete object), if the static type of the operand is different from its dynamic type, the static type shall be a base class of the operand’s dynamic type and the static type shall have a virtual destructor or the behavior is undefined.

The solution is either to declare all of the destructors virtual, or not to delete the objects through a pointer to One.