Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Memory and Utilities

RAII in C++ (resource acquisition is initialization)

RAII ties a resource's lifetime to an object: acquire in the constructor, release in the destructor. Learn it with lock guards, scope guards and exceptions.

RAII, short for resource acquisition is initialization, is the central C++ idiom for managing anything that must be released: memory, files, locks, sockets, transactions. A resource is owned by an object. The constructor acquires it, the destructor releases it. Because destructors run automatically when a scope ends, whether by reaching the closing brace, return, break or an exception, cleanup cannot be forgotten.

It is a concept rather than a library feature, but the standard library is full of it: std::vector, std::string, std::unique_ptr, std::fstream, std::lock_guard, std::jthread.

Try it

C++ A guard object that always cleans up

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.

C++ Writing your own owner and using the standard ones

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.

How it works

  1. Acquire in the constructor. If acquisition fails, throw; a constructor that throws leaves no half-built object, and members already built are destroyed.
  2. Release in the destructor. It runs for every normal exit and during stack unwinding. It should not throw: destructors are noexcept by default.
  3. Control copying. Decide what copying means. Usually the owner is move-only (delete the copy operations, provide a move), or copying makes a deep copy, or ownership is shared with a count (shared_ptr). This is the rule of five.
  4. Scope is lifetime. Locals are destroyed in reverse order of declaration; members in reverse order of their declaration in the class.

Usage notes

  • Do not write new and delete pairs in application code. If a function needs delete in a cleanup branch, wrap the resource instead.
  • The standard classes you should reach for first: std::unique_ptr (single owner), std::shared_ptr, std::vector and std::string (memory), std::lock_guard / std::scoped_lock / std::unique_lock (mutexes), std::ifstream / std::ofstream (files), std::jthread (joins in its destructor).
  • A "scope guard" runs arbitrary code on exit. C++ has no standard one yet (std::experimental::scope_exit is a TS), but a small class that stores a callable is easy to write.
  • Name the guard! std::lock_guard<std::mutex>(m); creates and immediately destroys a temporary, which locks nothing for the rest of the scope.
  • Exceptions: because cleanup is in destructors, you rarely need try/finally. C++ has no finally keyword for this reason.
  • A resource that is not released by the end of main because std::exit or std::abort was called will skip local destructors. Return from main or throw instead.
  • A destructor that throws during unwinding calls std::terminate.