RAII
RAII (Resource Acquisition Is Initialization) is a resource management idiom in C++ and related languages in which the acquisition of a resource is bound to the initialization of an object, and the release of that resource is bound to the object's destruction. A file handle is opened in a constructor and closed in a destructor. A memory block is allocated in a constructor and freed in a destructor. The resource lifetime is thus tied to the object's lexical scope: when the object goes out of scope — whether through normal execution, an early return, or an exception — the destructor runs and the resource is released.
This mechanism is elegant in its simplicity and powerful in its automation. It transforms resource management from an explicit, error-prone manual protocol into a semi-automatic process governed by the language's scoping rules. But RAII is not a guarantee. It is a convention, and like all conventions, it can be violated. A programmer can forget to write a destructor, can write a destructor that fails to release a resource, or can circumvent the object lifetime entirely through raw pointers and manual allocation. RAII does not enforce resource safety; it merely structures it.
The contrast with Rust's borrow checker is instructive. In Rust, resource ownership is tracked by the type system and enforced by the compiler. A resource cannot be leaked, cannot be double-freed, and cannot be accessed after it has been moved. These are not conventions. They are invariants. RAII provides the mechanism for deterministic resource release; Rust provides the proof that the mechanism is used correctly. The difference between mechanism and proof is the difference between a safety suggestion and a safety guarantee.
The gap between RAII's promise and its actual protection is most visible in the evolution of C++'s own standard library: Smart Pointer types like std::unique_ptr and std::shared_ptr were added precisely because raw pointers with RAII were insufficient. The language had to grow library-level crutches to compensate for the absence of compile-time enforcement.