0Pricing
C++ Academy · Lesson

Destructors and Cleanup

Release resources automatically.

Destructors and Cleanup is a free C++ Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What a Destructor Does

A destructor runs when an object's lifetime ends. Its job is to release whatever the object owns. It is named ~ClassName.

#include <iostream>

class File {
public:
    ~File() { std::cout << "closing file\n"; }
};

int main() {
    File f;
}   // ~File runs here

Freeing Owned Memory

If a class owns heap memory, its destructor must free it. This is the classic place to call delete.

#include <iostream>

class Buffer {
    int* data;
public:
    Buffer(int n) : data(new int[n]) {}
    ~Buffer() { delete[] data; }   // cleanup
};

int main() { Buffer b(10); }

Destructors Run Automatically

You do not call destructors yourself for stack objects — the compiler inserts the call at the end of scope.

Reverse Order of Members

Member objects are destroyed in the reverse order they were declared, after the destructor body runs.

#include <iostream>

struct A { ~A() { std::cout << "~A\n"; } };
struct B { ~B() { std::cout << "~B\n"; } };
struct C {
    A a;
    B b;          // destroyed before a
    ~C() { std::cout << "~C\n"; }
};

int main() { C c; }

Virtual Destructors

If you delete a derived object through a base pointer, the base destructor must be virtual — otherwise the derived part leaks.

#include <iostream>

struct Base {
    virtual ~Base() { std::cout << "~Base\n"; }
};
struct Derived : Base {
    ~Derived() override { std::cout << "~Derived\n"; }
};

int main() {
    Base* p = new Derived();
    delete p;   // both run thanks to virtual
}

Destructors Should Not Throw

Throwing from a destructor during stack unwinding terminates the program. Destructors should be marked (implicitly) noexcept and never let exceptions escape.

The Default Destructor

If you write none, the compiler generates a destructor that destroys each member. For classes with only RAII members, that is exactly right.

#include <string>
#include <vector>

struct Record {
    std::string name;          // self-cleaning
    std::vector<int> values;   // self-cleaning
};   // default destructor is enough

= default Destructor

Write ~Class() = default; to explicitly request the compiler-generated one — useful when you need a virtual destructor but no custom logic.

#include <iostream>

struct Shape {
    virtual ~Shape() = default;   // virtual + default
};

Releasing Non-Memory Resources

Destructors close files, release locks, and disconnect sockets too — not just free memory. Any acquired resource belongs here.

Order Across Objects

Multiple locals are destroyed in reverse construction order, so a destructor can safely use objects created before it.

Let RAII Members Do the Work

If every member is an RAII type, you often need no custom destructor at all. Reserve hand-written destructors for raw resources.

Quick Check

Test your understanding of destructors.

Recap

You learned that destructors release owned resources automatically at end of lifetime, run members in reverse declaration order, must not throw, and need to be virtual in polymorphic base classes. RAII members often make custom destructors unnecessary.

Frequently asked questions

Is the “Destructors and Cleanup” lesson free?

Yes — the full text of “Destructors and Cleanup” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “Destructors and Cleanup”?

Release resources automatically. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Destructors and Cleanup” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The RAII Principle
  2. Destructors and Cleanup
  3. Rule of Three/Five
  4. Scope Guards
← Back to C++ Academy