peterdemin / virtual-destruction-5-cents

My 5 cents to C++ virtual destructors (Examples)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

virtual-destruction-5-cents

Overview

According to StackOverflow question The general rule can be expressed in pseudo-code:

if static type of object is different from its dynamic type:
    if the static type is a base class of the dynamic type:
        if the static type has a virtual destructor:
            We're fine - dynamic type's destructor will be called
        else:
            behavior is undefined [1]
    else:
        behavior is undefined [2]

[1] Code will compile without warnings and probably will work fine, but it's not guaranteed and may cause entangled error at runtime.

[2] That means code is something like:

class A {};
class B {};
B *a = (B*)(new A());
delete a;

Scenarios

Scenario 1: Entangled undefined behaviour

Given Base class has no virtual members
And Base's destructor is not virtual
And Derived class has no virtual members
When you delete derived class by pointer to base class (e.g. Base *pb = Derived(); delete pb;)
Then Derived's destructor isn't called
But no memory leak occurs.
Scenario 2: Entangled undefined behaviour

Given Base class has virtual member
And Base's destructor is not virtual
And Derived class has another more virtual member
When you delete derived class by pointer to base class (e.g. Base *pb = Derived(); delete pb;)
Then Derived's destructor isn't called
But no memory leak occurs.
Scenario 3: The correct one

Given Base class has virtual member
And Base's destructor is virtual
And Derived class has another more virtual member
When you delete derived class by pointer to base class (e.g. Base *pb = Derived(); delete pb;)
Then Derived's destructor is called
And no memory leak occurs.
Scenario 4: Runtime error

Given Base class has no virtual member
And Base's destructor is not virtual
And Derived class has virtual member
When you delete derived class by pointer to base class (e.g. Base *pb = Derived(); delete pb;)
Then Derived's destructor is not called
Derived's virtual table leaks memory which causes assertion fail.
Scenario 5: Memory leak

Given Base class has virtual member
But Base's destructor is not virtual
And Derived class has one more dynamic member
When you delete derived class by pointer to base class (e.g. Base *pb = Derived(); delete pb;)
Then Derived's destructor isn't called
And new member leaks memory.

How to build

mkdir build
cd build
cmake ..

About

My 5 cents to C++ virtual destructors (Examples)


Languages

Language:C++ 100.0%