Resource acquisition is initialization
Appearance
Resource Acquisition Is Initialization (often referred to by the acronym RAII) is a technique in object oriented programming.
The technique states that, like the name implies, objects should initialise and release all resources they acquire.
It is of paramount importance especially in languages that do not support built-in garbage collection, such as C++.
A stereotypical example of RAII is the Standard Template Library's file-stream class. An input file stream is opened in the object's constructor, and it is closed upon destruction of the object. Since C++ allows objects to be allocated on the stack, C++'s scoping mechanism can be used to control file access.
External links
- Article "The RAII Programming Idiom" by Jon Hanna
- Descriptions from Portland Pattern Repository
- Sample Chapter "Gotcha #67: Failure to Employ Resource Acquisition Is Initialization" by Stephen Dewhurst
- Article "A Conversation with Bjarne Stroustrup" by Bill Venners
- Article "The Law of The Big Two" by Bjorn Karlsson and Matthew Wilson
- RAII in C++ by Jonathan Dodds
- Article "Implementing the 'Resource Acquisition is Initialization' Idiom" by Danny Kalev
- Article "RAII" by Mike Nordell