Resource Acquisition Is Initialization

Resource allocation is initialization, usually abbreviated by RAII, for resource acquisition is initialization English, refers to a certain programming languages ​​( such as C ) common programming technique for the management of resources (also called resources ). The allocation of resources is bound to the constructor of a variable of a custom type. The automatic release is triggered by the out of scope (at the end of the block, with the exception of tripping, it is returned to the caller, etc. ), and the implicit destructor of the variables then provides for the re- release of the resource.

Typical applications for RAII are the control of process or thread locks in concurrent programs and the management of file operations. Since destructors are called automatically even under exceptional conditions, RAII is also a key concept for writing exception -strength code.

Among the programming languages ​​that allow the application of RAII programming technique include, for example C , Ada and D. In C # or Java, this technique is, however, because the garbage collector does not directly possible (see # variants).


The following sample program is written in the programming language C :

# include # include   class file {      FILE * datei_;   public:      File (const std :: string & name )     : Datei_ (std :: fopen ( name.c_str (), " w " )) { } / / open the file        ~ File () {           std :: fclose ( datei_ ); / / Close the file      }        void output (const std :: string & text) {          if ( datei_ )              std :: fputs ( text.c_str () datei_ );      } };   int main () {      File file ( " aufzeichnung.txt "); / / Open the file ( requesting the resource)      datei.Ausgabe ( "Hello World! ");        / / With the end of the function ends and the scope (Scope )      / / The object file. Therefore, the destructor will file :: ~ File ()      / / Called to close the file → release of the resource. } variants

The correct functioning of this technique depends crucially on the properties of the constructors and destructors of the language. In C is guaranteed by the language standard that an object as it passes through its declaration creates, while its constructor is called. When leaving its scope, the object must be destroyed, ie its destructor is called and can cause the release of resources.

Some programming languages ​​with garbage collection, such as C # or Java, it does not guarantee. The time at which an object is destroyed and the finalization method is called, is then no longer deterministic. Thus, the object can occupy a resource longer than actually expected, ie via its validity range. In general, this problem can only be circumvented by a function to release the resources is called explicitly or special language constructs are used.

The recommended alternative is for C #, the implementation of System.IDisposable interfaces - also called Dispose Pattern. When using the keyword using ensures that the Dispose () method is called at the end of the valid range, and thus occupied resources at a defined time to be released.

In Java, can be ensured by using the statement try -with -resources - similar objects from being destroyed again at the end of a scope in reverse order.