RAII – Resource Aquisition is Initialization

RAII is a design pattern implemented by C++ programming language. Under it, resources are represented by objects, and are acquired on initialization and released on destruction. The last is one of the most useful property and that is not present in languages like Java. In this pattern, an object is destroyed whenever it goes out of scope or an exception in thrown.

The capability of recovering from exception is particularly useful when the resource is shared, for example lock. If the destructor was not called, then a deadlock could occur. If the resource was a dynamic allocated memory, we would have a memory leak.

Further reading

[1] http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization
[2] http://www.codeproject.com/KB/cpp/RAIIFactory.aspx

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s