Resource Acquisition is Initialization (RAII) is commonly used in C++ to manage the lifetimes of resources which require some manner of cleanup code at the end of their lifetime, from delete
ing new
ed pointers to releasing file handles.
How do I quickly and easily use RAII to manage the lifetime of a resource I acquire from a C-style API?
In my case, I want to use RAII to automatically execute a cleanup function from a C-style API when the variable holding the C-style resource it releases goes out of scope. I don't really need additional resource wrapping beyond that, and I'd like to minimize the code overhead of using RAII here. Is there a simple way to use RAII to manage resources from a C-style API?
How to encapsulate C api into RAII C++ classes? is related, but I don't believe it's a duplicate--that question is regarding more complete encapsulation, while this question is about minimal code to get the benefits of RAII.
There is an easy way to use RAII to manage resources from a C-style interface: the standard library's smart pointers, which come in two flavors:
std::unique_ptr
for resources with a single owner and the team ofstd::shared_ptr
andstd::weak_ptr
for shared resources. If you're having trouble deciding which your resource is, this Q&A should help you decide. Accessing the raw pointer a smart pointer is managing is as easy as calling itsget
member function.If you want simple, scope-based resource management,
std::unique_ptr
is an excellent tool for the job. It's designed for minimal overhead, and is easy to set up to use custom destruction logic. So easy, in fact, that you can do it when you declare the resource variable:acquireResource
executes where you call it, at the start of the variable's lifetime.releaseResource
will execute at the end of the variable's lifetime, usually when it goes out of scope.1 Don't believe me? you can see it in action on Coliru, where I've provided some dummy implementations to the acquire and release functions so you can see it happening.You can do much the same with
std::shared_ptr
, if you require that brand of resource lifetime instead:Now, both of these are all well and good, but the standard library has
std::make_unique
2 andstd::make_shared
and one of the reasons is further exception-safety.GotW #56 mentions that the evaluation of arguments to a function are unordered, which means if you have a function that takes your shiny new
std::unique_ptr
type and some resource that might throw on construction, supplying a that resource to a function call like this:means that the instructions might be ordered like this:
acquireResource
ThrowsOnConstruction
std::unique_ptr
from resource pointerand that our precious C interface resource won't be cleaned up properly if step 2 throws.
Again as mentioned in GotW #56, there's actually a relatively simple way to deal with the exception safety problem. Unlike expression evaluations in function arguments, function evaluations can't be interleaved. So if we acquire a resource and give it to a
unique_ptr
inside a function, we'll be guaranteed no tricky business will happen to leak our resource whenThrowsOnConstruction
throws on construction. We can't usestd::make_unique
, because it returns astd::unique_ptr
with a default deleter, and we want our own custom flavor of deleter. We also want to specify our resource acquisition function, since it can't be deduced from the type without additional code. Implementing such a thing is simple enough with the power of templates:3Live on Coliru
you can use it like this:
and call
func
worry-free, like this:The compiler can't take the construction of
ThrowsOnConstruction
and stick it between the call toacquireResource
and the construction of theunique_ptr
, so you're good.The
shared_ptr
equivalent is similarly simple: just swap out thestd::unique_ptr<T, Deletion>
return value withstd::shared_ptr<T>
, and change the name to indicate a shared resource:4Use is once again similar to the
unique_ptr
version:and
Edit:
As mentioned in the comments, there's a further improvement you can make to the use of
std::unique_ptr
: specifying the deletion mechanism at compile time so theunique_ptr
doesn't need to carry a function pointer to the deleter when it's moved around the program. Making a stateless deleter templated on the function pointer you're using requires four lines of code, placed beforemake_c_handler
:Then you can modify
make_c_handler
like so:The usage syntax then changes slightly, to
Live on Coliru
make_c_shared_handler
would not benefit from changing to a templated deleter, asshared_ptr
does not carry deleter information available at compile time.1. If the value of the smart pointer is
nullptr
when it's destructed, it won't call the associated function, which is quite nice for libraries which handle resource release calls with null pointers as error conditions, like SDL.2.
std::make_unique
was only included in the library in C++14, so if you're using C++11 you might want to implement your own--it's very helpful even if it's not quite what you want here.3. This (and the
std::make_unique
implementation linked in 2) depend on variadic templates. If you're using VS2012 or VS2010, which have limited C++11 support, you don't have access to variadic templates. The implementation ofstd::make_shared
in those versions was instead made with individual overloads for each argument number and specialization combination. Make of that what you will.4.
std::make_shared
actually has more complex machinery than this, but it requires actually knowing how big an object of the type will be. We don't have that guarantee since we're working with a C-style interface and may only have a forward declaration of our resource type, so we won't be worrying about it here.A dedicated scope guard mechanism can cleanly and concisely manage C-style resources. Since it's a relatively old concept, there are a number floating around, but scope guards which allow arbitrary code execution are by nature the most flexible of them. Two from popular libraries are
SCOPE_EXIT
, from facebook's open source libraryfolly
(discussed in Andrei Alexandrescu talk on Declarative Control Flow), andBOOST_SCOPE_EXIT
from (unsurprisingly) Boost.ScopeExit.folly's
SCOPE_EXIT
is part of a triad of declarative control flow functionality provided in<folly/ScopeGuard.hpp>
.SCOPE_EXIT
SCOPE_FAIL
andSCOPE_SUCCESS
respectively execute code when control flow exits the enclosing scope, when it exits the enclosing scope by throwing an exception, and when it exits without throwing an exception.1If you have a C-style interface with a resource and lifetime management functions like this:
you may use
SCOPE_EXIT
like so:Boost.ScopeExit has a slightly differing syntax.2 To do the same as the above code:
You may find it suitable in both cases to declare
resource
asconst
, to ensure you don't inadvertently change the value during the rest of the function and re-complicate the lifetime management concerns you're trying to simplify.In both cases,
releaseResource
will be called when control flow exits the enclosing scope, by exception or not. Note that it will also be called regardless of whetherresource
isnullptr
at scope end, so if the API requires cleanup functions not be called on null pointers you'll need to check that condition yourself.The simplicity here versus the use of a smart pointer comes at the cost of not being able to move your lifetime management mechanism around the enclosing program as easily as you can with smart pointers, but if you want a dead-simple guarantee of cleanup execution when the current scope exits, scope guards are more than adequate for the job.
1. Executing code only on success or failure offers a commit/rollback functionality which can be incredibly helpful for exception safety and code clarity when multiple failure points may occur in a single function, which seems to be the driving reason behind the presence of
SCOPE_SUCCESS
andSCOPE_FAIL
, but you're here because you're interested in unconditional cleanup.2. As a side note, Boost.ScopeExit also doesn't have built-in success/fail functionality like folly. In the documentation, success/fail functionality like that provided by the folly scope guard is instead implemented by checking a success flag that has been captured by reference. The flag is set to
false
at the start of the scope, and set totrue
once the relevant operations succeed.