RAII: Resource Acquisition is Initialisation

I recently ran into what I assume is a common problem for new C++ programmers, I needed to allocate some memory on the heap and ensure it was freed up when I was finished with it. The specific function I was working on was intended to query a service for its start type using Microsoft's QueryServiceStatus. My initial implementation was fairly horrible and prone to memory leaks as it didn't guarantee the allocated memory would get freed when it went out of scope. A colleague recommended I take a look at RAII and it's uses. Once I got my head around it I was able to apply it to my problem, and resolved the issues with my code. So, I'll try and explain it here with a simpler example.

RAII stands for Resource Acquisition Is Initialisation. The core principle of RAII is that we want to tie the lifetime of a particular resource to an object on the stack, such that when our object goes out of scope the object's destructor cleans up our resource.

Here's a simple example where RAII could be used:

Here we use ofstream to create an output steam object and associate our shopping_list.txt file with it by calling .open(). We then write our shopping items to the file, before closing the output stream. Ok, lets say we hit an exception when writing to the file and our function returns early, before we reach our .close() operation. Now our ofstream object is sitting in memory and we've no reference to it any more, and lets not forget - the file is still open.

So, the solution is RAII. We want to encapsulate our ofstream object in our own class which deals with the destruction of the object automatically when our ofstream object goes out of scope, which looks something like this:

So, we've got ourselves a RAII class which sets up our ofstream object with a file association in the constructor. We then call the writeToShoppingList() method to write out our shopping data, and we then simply return. Because we initialised our RAII class object on the stack we know that it will get destroyed when it goes out of scope, so we no longer have to worry about calling .close() on our shopping_list.txt file because our ShoppingList object's destructor does that work for us.

So, using RAII we've made our simple program less error-prone and removed the worry about ensuring the ofstream object is closed.

Comments

Popular posts from this blog

Err.exe: A Tool for Error Code Lookup