RAII, present in languages like C++ and Rust, automatically releases resources when they go out of scope, alleviating the need for manual management. This feature significantly reduces boilerplate code. Also, human are very bad at remembering releasing resources even with language facilities such as defer 1.

Implementations

Shortcoming of RAII

Discourage Bulking

One criticism of RAII is that it encourages binding lifetime of resources with lifetime of each individual domain object. This often is not the most optimal solution performance-wise. 1 Related, RAII encourage symmetric of allocation and deallocation, when it can often be more beneficial to free all memory at once (both garbage collector or arena allocator achieve that). 2

Destructors and Exceptions

When combining RAII and exceptions, the usual requirement is that the destructors can never throw. However, this cause the problem for types like scope_guard which unfortunately just can’t be standardized in C++. 3 4

Signaling Failure in Destructors

There are no way for destructors to communicate information outward (besides throwing exceptions), so as a result C++ standard library often decides to silently swallow errors in destructors 4

Footnotes

  1. Zig defer Patterns (note) 2

  2. Untangling Lifetimes: The Arena Allocator - by Ryan Fleury

  3. Why Not Just Do Simple C++ RAII in C? | The Pasture

  4. N3199: Improved __attribute__((cleanup)) Through defer 2