Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is meant by Resource Acquisition is Initialization (RAII)?

Tags:

c++

raii

What is meant by Resource Acquisition is Initialization (RAII)?

like image 758
John Avatar asked Feb 23 '10 20:02

John


People also ask

What is RAII in programming?

Resource Acquisition Is Initialization or RAII, is a C++ programming technique which binds the life cycle of a resource that must be acquired before use (allocated heap memory, thread of execution, open socket, open file, locked mutex, disk space, database connection—anything that exists in limited supply) to the ...

What is RAII object?

The principle that objects own resources is also known as "resource acquisition is initialization," or RAII. When a resource-owning stack object goes out of scope, its destructor is automatically invoked. In this way, garbage collection in C++ is closely related to object lifetime, and is deterministic.

Why is RAII important?

RAII avoids using objects in an invalid state. it already makes life easier before we even use the object. There are three error cases to handled: no file can be opened, only one file can be opened, both files can be opened but copying the files failed.

What is an advantage of using RAII over not using RAII?

Advantages of RAIIThe instance will use move constructors and move assignments to correctly transfer ownership of the resource. You don't have to worry about the complex implementation of managing that resource through encapsulation. It also provides exception safety in your code.


1 Answers

It's a really terrible name for an incredibly powerful concept, and perhaps one of the number 1 things that C++ developers miss when they switch to other languages. There has been a bit of a movement to try to rename this concept as Scope-Bound Resource Management, though it doesn't seem to have caught on just yet.

When we say 'Resource' we don't just mean memory - it could be file handles, network sockets, database handles, GDI objects... In short, things that we have a finite supply of and so we need to be able to control their usage. The 'Scope-bound' aspect means that the lifetime of the object is bound to the scope of a variable, so when the variable goes out of scope then the destructor will release the resource. A very useful property of this is that it makes for greater exception-safety. For instance, compare this:

RawResourceHandle* handle=createNewResource(); handle->performInvalidOperation();  // Oops, throws exception ... deleteResource(handle); // oh dear, never gets called so the resource leaks 

With the RAII one

class ManagedResourceHandle { public:    ManagedResourceHandle(RawResourceHandle* rawHandle_) : rawHandle(rawHandle_) {};    ~ManagedResourceHandle() {delete rawHandle; }    ... // omitted operator*, etc private:    RawResourceHandle* rawHandle; };  ManagedResourceHandle handle(createNewResource()); handle->performInvalidOperation(); 

In this latter case, when the exception is thrown and the stack is unwound, the local variables are destroyed which ensures that our resource is cleaned up and doesn't leak.

like image 102
the_mandrill Avatar answered Oct 13 '22 12:10

the_mandrill