C++ Reference ยท Types and Language Concepts
Destructors and RAII
Reference for C++ destructors and RAII: when destructors run, destruction order, virtual destructors, noexcept, exceptions during unwinding and scope guards.
A destructor is the member function ~ClassName() that runs automatically when an object's lifetime ends: at the end of its scope, when a delete is applied, when its owner is destroyed, or when the program exits for statics. It releases whatever the object holds: memory, file handles, locks, sockets.
RAII (resource acquisition is initialization) is the idiom built on that guarantee. You acquire the resource in a constructor and release it in the destructor, so cleanup happens on every exit path, including return, break and exceptions. Standard types such as std::vector, std::unique_ptr, std::fstream and std::lock_guard are all RAII classes.
{
std::lock_guard<std::mutex> guard(m); // locks
work(); // may throw
} // unlocks, always
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
~ClassName(); // user-declared destructor ~ClassName() = default; // explicitly default virtual ~Base() = default; // for polymorphic bases ~ClassName() noexcept(false); // rare: allow it to throw
| Part | Description |
|---|---|
| Name | Tilde plus class name. No parameters, no return type, cannot be overloaded. |
| Implicit | If you declare none, the compiler generates one that destroys members and bases. It is implicitly noexcept in most cases. |
| Order | The destructor body runs first, then members in reverse declaration order, then base classes in reverse order of construction. |
Returns: nothing.
Errors / undefined behavior: deleting a derived object through a base pointer whose destructor is not virtual is undefined behavior. Using an object after its destructor ran is undefined. If a destructor exits via an exception while another exception is already unwinding, std::terminate is called.
Usage notes
- Destructors are
noexceptby default. An exception leaving one callsstd::terminate. Catch and swallow (or log) errors inside, or provide an explicitclose()that can report failure. - Local objects die in reverse order of construction. Temporaries die at the end of the full expression, unless bound to a reference that extends their lifetime.
- Virtual destructors. Any class meant to be a base for polymorphic deletion needs
virtual ~Base() = default;. If a class has no virtual functions and is not a base, you do not need one. - Exceptions and unwinding. When an exception propagates, all fully constructed locals are destroyed on the way out. That is why RAII beats manual cleanup code.
- Rule of zero. If every resource is held in a standard RAII type, you write no destructor at all. See Rule of three, five and zero.
- Do not call
delete thiscasually, and never call a destructor explicitly except in placement-new code (p->~T();). - Statics and globals are destroyed after
mainreturns, in reverse order of construction. A destructor that touches another static can hit one that is already gone. std::exitandabort.exitruns static destructors but not locals;std::abortandquick_exitskip them.- Scope guards.
std::unique_ptrwith a custom deleter, or a small class that stores a callable, turns any cleanup into RAII. - Standard containers require the destructors of their element types not to throw.