From cppreference.com
Resource Acquisition Is Initialization or RAII, is a C++ programming technique
which binds the life cycle of a resource that must be acquired before use (allocated heap memory, thread of execution, open socket, open file, locked mutex, disk space, database connection—anything that exists in limited supply) to the
of an object.
RAII guarantees that the resource is available to any function that may access the object (resource availability is a
, eliminating redundant runtime tests). It also guarantees that all resources are released when the lifetime of their controlling object ends, in reverse order of acquisition. Likewise, if resource acquisition fails (the constructor exits with an exception), all resources acquired by every fully-constructed member and base subobject are released in reverse order of initialization. This leverages the core language features (
,
,
and
) to eliminate resource leaks and guarantee exception safety. Another name for this technique is Scope-Bound Resource Management (SBRM), after the basic use case where the lifetime of an RAII object ends due to scope exit.
RAII can be summarized as follows:
encapsulate each resource into a class, where
the constructor acquires the resource and establishes all class invariants or throws an exception if that cannot be done,
the destructor releases the resource and never throws exceptions;
always use the resource via an instance of a RAII-class that either
has automatic storage duration or temporary lifetime itself, or
has lifetime that is bounded by the lifetime of an automatic or temporary object.
Move semantics enable the transfer of resources and ownership between objects, inside and outside containers, and across threads, while ensuring resource safety.
(since C++11)Classes with open()/close(), lock()/unlock(), or init()/copyFrom()/destroy() member functions are typical examples of non-RAII classes:
std::mutexm;voidbad(){m.lock();// acquire the mutexf();// if f() throws an exception, the mutex is never releasedif(!everything_ok())return;// early return, the mutex is never releasedm.unlock();// if bad() reaches this statement, the mutex is released}voidgood(){std::lock_guard<std::mutex>lk(m);// RAII class: mutex acquisition is initializationf();// if f() throws an exception, the mutex is releasedif(!everything_ok())return;// early return, the mutex is released}// if good() returns normally, the mutex is releasedThe standard library
The C++ library classes that manage their own resources follow RAII:
,
,
(since C++20), and many others acquire their resources in constructors (which throw exceptions on errors), release them in their destructors (which never throw), and don't require explicit cleanup.
Notes
RAII does not apply to the management of the resources that are not acquired before use: CPU time, core availability, cache capacity, entropy pool capacity, network bandwidth, electric power consumption, stack memory. For such resources, a C++ class constructor cannot guarantee resource availability for the duration of object lifetime, and other means of resource management have to be used.
External links