Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usefulness of RAII without exceptions

I recently found about RAII in c++ and most examples of RAII talk about exception safety. How you can always release resources even if an exception were to be thrown.

The question I have is, if RAII is worth it if you do not have exceptions turned on. In our firm we work on embedded projects for arm and exceptions are turned off by default and we don't really see any need for them.

Thanks for all the answers!

like image 442
rozina Avatar asked Jul 17 '13 19:07

rozina


People also ask

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

Smart pointers use RAII to hide the manipulation of pointers, which are a lower level than business code, so RAII helps respect levels of abstraction in that case too. This is true for resource management in general, including database connection.

What is RAII and why is it important?

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 the use of RAII in C++ programming?

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.

When did C++ add RAII?

RAII is associated most prominently with C++ where it originated, but also D, Ada, Vala, and Rust. The technique was developed for exception-safe resource management in C++ during 1984–89, primarily by Bjarne Stroustrup and Andrew Koenig, and the term itself was coined by Stroustrup.


3 Answers

RAII with exceptions is basically a requirement.

RAII without exceptions means that you can couple the allocation of resources with the code to dispose the resources.

This lets you have functions with multiple exit points, simplifies the writing of destructors (often destructors in an RAII heavy environment are empty or default), can simplify object assignment and moving (once again, often empty or default with sufficient RAII work).

A classic example for embedded environments would be locking and unlocking some mutex. You want to guarantee that you don't lock a mutex and forget to unlock it. To do this, code discipline means you have to have basically one exit point from your function, and you have to engage in gymnastics sometimes to ensure that this happens.

With RAII, you just create a RAII resource holder that owns the lock. Now you can return whenever you want, and the code to unlock the resource is automatically injected at the return site. Code flow is simplified, and resource leaks are far less common.

RAII is also amazing documentation. A structure or class with a Foo* could mean anything: how and when you are supposed to deal with that resource is unclear. A structure or class with a std::unique_ptr<Foo> is clearly owning that pointer. Functions that take std::unique_ptr<Foo> are clearly taking ownership over the passed in pointer. Functions that return std::unique_ptr<Foo> are clearly giving you ownership of that pointer.

like image 180
Yakk - Adam Nevraumont Avatar answered Sep 18 '22 17:09

Yakk - Adam Nevraumont


RAII in C++ is a much more wide notion. It is an idiom which allows you to write safer code. Resource acquisition part of RAII is where you begin something that must be ended later, such as opening a file and closing it later, allocating some memory and deallocating it, acquiring and releasing a lock. RAII relates to such importants notions as: smart pointers, thread-safety (controlling mutex locks in multi-threaded programs - http://www.boost.org/doc/libs/1_49_0/doc/html/boost/interprocess/scoped_lock.html here is an example of RAII), interaction with files , object ownership (when you use smart pointers like unique_ptr you already use RAII) and so on.

So RAII is always worth to use in good C++ code regardless of exceptions.

like image 44
Oleksandr Karaberov Avatar answered Sep 18 '22 17:09

Oleksandr Karaberov


The question I have is, if RAII is worth it if you do not have exceptions turned on.

Of course it's worth it! Exception safety is only one aspect of RAII. Another one for example is to avoid leaking dynamically created instances.

like image 39
πάντα ῥεῖ Avatar answered Sep 20 '22 17:09

πάντα ῥεῖ