Topic 82 of 91 11. Additional C++ Topics Added Interactive Tutorial

Virtual Destructors in C++: Safe Cleanup Through Base-Class Pointers

7 mins read Last updated: 26 Sep 2026 19,234 views

Direct Answer & Core Concept

When deleting a dynamically allocated derived class object through a pointer to its base class, if the base class destructor is not virtual, only the base destructor is called, skipping the derived destructor entirely. This causes Undefined Behavior and severe memory/resource leaks.

Detailed Explanation & Mechanics

1. Memory Leak Mechanics (Non-Virtual Destructor Bug):

Architecture Diagram
Base* ptr = new Derived();
delete ptr;

NON-VIRTUAL DESTRUCTOR:
Calls Base::~Base() ONLY ──► Derived members & memory leaked! (UB)

VIRTUAL DESTRUCTOR:
Calls Derived::~Derived() ──► Calls Base::~Base() ──► 100% Clean!

2. Standard Guideline:

A base class destructor should be either public virtual (for polymorphic deletion) or protected non-virtual (if polymorphism is not used for deletion).

Runnable Code Example: Virtual Destructors in C++

C++20 / C++23
main.cpp
Run Online
#include <iostream>
#include <memory>

class SafeBase {
public:
    SafeBase() { std::cout << "SafeBase allocated.\n"; }
    // Virtual destructor guarantees full derived destruction
    virtual ~SafeBase() { std::cout << "SafeBase destroyed.\n"; }
};

class HeavyDerived : public SafeBase {
private:
    int* largeBuffer;
public:
    HeavyDerived() : largeBuffer(new int[1000]) {
        std::cout << "HeavyDerived allocated 4000 bytes on heap.\n";
    }
    ~HeavyDerived() override {
        delete[] largeBuffer;
        std::cout << "HeavyDerived freed heap memory safely.\n";
    }
};

int main() {
    std::cout << "--- Polymorphic Deletion via Base Pointer ---\n";
    SafeBase* polyPtr = new HeavyDerived();
    delete polyPtr; // Triggers HeavyDerived dtor first, then SafeBase dtor
    std::cout << "--- Deletion Complete ---\n";
    return 0;
}

Common C++ Technical Interview Questions & Answers

Interview Prep
Q

Q: What is the compiler warning when a class with virtual functions lacks a virtual destructor?

A: Compilers emit `-Wnon-virtual-dtor`, warning that deleting an object through a pointer to this base class will cause undefined behavior.
Q

Q: When is it acceptable for a base class to have a non-virtual destructor?

A: When the class is not intended to be used polymorphically for dynamic deletion, and its destructor is declared `protected` to prevent direct `delete` on base pointers.

Key Takeaways, Modern Best Practices & Idioms

C++ Best Practices & Production Rules

• Always declare the destructor `virtual` in any class that has at least one virtual function. • Use `= default` for virtual destructors when no custom cleanup logic is required. • Use smart pointers (`std::unique_ptr`) which automatically execute virtual destructors correctly.
We value your privacy & transparency

OperateTools uses cookies and local storage to ensure basic tool functionality, preserve preferences, and analyze aggregated traffic. We never sell your personal data or index your document files. Learn more in our Privacy Policy.