| Resource Acquisition Is Initialization |
Article Index for Resource |
Shopping Acquisition |
Website Links For Resource |
Information AboutResource Acquisition Is Initialization |
| CATEGORIES ABOUT RESOURCE ACQUISITION IS INITIALIZATION | |
| object-oriented programming | |
| articles with example c code | |
|
Resource Acquisition Is Initialization, often referred to by the acronym '''RAII''', is a popular Design Pattern in C++ and D . The technique combines acquisition and release of resources with initialization and uninitialization of objects. TYPICAL USES The RAII technique is often used for controlling thread locks in multi-threaded applications. Another typical example of RAII is file operations, e.g. the C++ Standard Library 's file-streams. 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. RAII is also used (as shown in the example below) to ensure exception safety. RAII makes it possible to avoid resource leaks without extensive use of try/catch blocks and is widely used in the software industry.The ownership of dynamically allocated memory (memory allocated with new) can be controlled with RAII. For this purpose, the C++ Standard Library defines . C++ EXAMPLE The following RAII class is a lightweight wrapper to the C Standard Library file system calls. #include class file { public:
if( !m_file_handle ) throw std::runtime_error("file open failure") ; } ~file() { if( std::fclose(m_file_handle) != 0 ) { // deal with filesystem errors, fclose() may fail when flushing latest changes } }
if( std::fputs(str, m_file_handle) == EOF ) throw std::runtime_error("file write failure") ; } private:
// copy and assignment not implemented; prevent their use by // declaring them private. file( const file & ) ; file & operator=( const file & ) ; } ; This RAII class can then be used as follows : void example_usage() { // open file (acquire resource) file logfile("logfile.txt") ; logfile.write("hello logfile!") ; // continue using logfile ... // throw exceptions or return without worrying about closing the log; // it is closed automatically when logfile goes out of scope. }
Automatic variables easily manage multiple resources within a single function: They are destructed in the reverse order of their construction, and an object is only destructed if fully constructed. That is, if no exception was thrown inside its constructor. Using RAII-enabled resources simplifies and reduces overall code size and helps ensure program correctness. RESOURCE MANAGEMENT IN OTHER LANGUAGES Java 's objects are garbage collected automatically at indeterminate times, and it has no equivalent to C++'s automatic variables. Resources must thus be manually closed by the programmer. The preceding example would be written like this void java_example() { // open file (acquire resource) LogFile logfile = new LogFile("logfile.txt") ; try { logfile.write("hello logfile!") ; // continue using logfile ... // throw exceptions or return without worrying about closing the log; // it is closed automatically when exiting this block } finally { // explicitely release the resource logfile.close(); } } The burden of releasing the resources falls on the programmer for each place the resource is used. Ruby and Smalltalk support RAII in the special case of scoped variables inside closure blocks. Here is an example in Ruby: |
|
|