Using Destructors for Resource Management

How can I use destructors to manage resources in C++?

Destructors in C++ are responsible for cleaning up when an object is destroyed. This makes them ideal for managing resources that need to be acquired during an object's lifetime and released when the object is no longer needed.

For example, consider a class that manages a dynamically allocated buffer:

#include <cstddef>

class DynamicBuffer {
public:
  DynamicBuffer(std::size_t size)
    : data_{new int[size]}, size_{size} {}

  ~DynamicBuffer() {
    delete[] data_;
  }

private:
  int* data_;
  std::size_t size_;
};

int main() {
  {
    DynamicBuffer buf(1024);
    // Usee buf...
  }
  // buf is destroyed here, calling destructor
}

Here, the constructor allocates memory for the buffer, and the destructor ensures that this memory is freed when the DynamicBuffer object goes out of scope.

This technique is known as Resource Acquisition Is Initialization (RAII). The idea is that a resource (like memory, a file handle, a network connection, etc.) should be acquired in the constructor (during initialization) and released in the destructor. This way, we can't forget to release the resource, and we don't have to manually manage the object's lifetime.

Some key points about destructors:

  • If you don't define a destructor, the compiler provides a default one (which does nothing).
  • The destructor is called automatically when an object is destroyed, either because it went out of scope or because delete was called on a pointer to the object.
  • Destructors don't take arguments and don't return a value.

So use destructors to clean up any resources that your object owns. This is a key part of writing safe, leak-free C++ code.

Classes, Structs and Enums

A crash tour on how we can create custom types in C++ using classes, structs and enums

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

When to Use Classes vs Structs in C++
What are the key differences between classes and structs in C++, and when should I use one over the other?
Multiple Constructors with the Same Name
How can a class have multiple constructors with the same name?
When to Use Public Inheritance
In what situations is it appropriate to use public inheritance in C++?
When to Use Member Initializer Lists
What are the benefits of using member initializer lists in constructors?
Implicitly-Defined Default Constructors
When does the compiler implicitly define a default constructor for a class?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant