What do you use RAII for?

D

dean

Hi,

What kinds of things have you used Resource Acquisition is
Initialization for? I've been using it in a few places and its
remarkably useful. Here is where I've use this currently:

1. Creating and/or maintaining hourglass cursors in and between
function calls.
2. Oracle transaction handling and rolling back on exception.
3. Setting bind variable lifetimes in an SQL library.
4. Opening and closing files.
5. Opening and closing database cursors.

Thanks for useful tips!

-Dean
 
P

Paul Brettschneider

dean said:
Hi,

What kinds of things have you used Resource Acquisition is
Initialization for? I've been using it in a few places and its
remarkably useful. Here is where I've use this currently:

1. Creating and/or maintaining hourglass cursors in and between
function calls.
2. Oracle transaction handling and rolling back on exception.
3. Setting bind variable lifetimes in an SQL library.
4. Opening and closing files.
5. Opening and closing database cursors.

* mutexes/rwlocks
 
Y

Yannick Tremblay

Hi,

What kinds of things have you used Resource Acquisition is
Initialization for? I've been using it in a few places and its
remarkably useful. Here is where I've use this currently:

1. Creating and/or maintaining hourglass cursors in and between
function calls.
2. Oracle transaction handling and rolling back on exception.
3. Setting bind variable lifetimes in an SQL library.
4. Opening and closing files.
5. Opening and closing database cursors.

Thanks for useful tips!

Everything (well almost).

RAII is one of the most important idiom in C++ and the great thing is
that it is incredibly versatile. Are you using standard containers?
Then you are using RAII.

Yan
 
M

Michael Oswald

dean said:
1. Creating and/or maintaining hourglass cursors in and between
function calls.
2. Oracle transaction handling and rolling back on exception.
3. Setting bind variable lifetimes in an SQL library.
4. Opening and closing files.
5. Opening and closing database cursors.

6. Debug-Logging entering and leaving of a function

lg,
Michael
 
J

James Kanze

What kinds of things have you used Resource Acquisition is
Initialization for? I've been using it in a few places and its
remarkably useful. Here is where I've use this currently:
1. Creating and/or maintaining hourglass cursors in and between
function calls.
2. Oracle transaction handling and rolling back on exception.
3. Setting bind variable lifetimes in an SQL library.
4. Opening and closing files.
5. Opening and closing database cursors.

It depends on just how far you're willing to stretch it. I
frequently use something more or less similar for transaction
management; if the destructor is called before the commit
function has been called, it does a rollback. I guess the
presence of the commit function means that it isn't strictly
speaking RAII, but in practice, while I often need scoped clean
up, except for resources like locks, the clean-up in the case of
errors is almost always different from the normal case. If
you're outputting to a file, for example, you'll remove the file
if it hasn't been correctly closed (committed), or if there was
an error on the close.
 
E

Erik Wikström

Hi,

What kinds of things have you used Resource Acquisition is
Initialization for? I've been using it in a few places and its
remarkably useful. Here is where I've use this currently:

1. Creating and/or maintaining hourglass cursors in and between
function calls.
2. Oracle transaction handling and rolling back on exception.
3. Setting bind variable lifetimes in an SQL library.
4. Opening and closing files.
5. Opening and closing database cursors.

Memory and file stream management mostly.
 
J

Jon Harrop

dean said:
What kinds of things have you used Resource Acquisition is
Initialization for?

RAII is incredibly useful because it handles a tiny number of the use cases
of higher-order functions. In the absence of first-class functions, RAII is
really your only choice.
 
R

Roland Pibinger

What kinds of things have you used Resource Acquisition is
Initialization for? I've been using it in a few places and its
remarkably useful. Here is where I've use this currently:

4. Opening and closing files.

Closing files in the destructor is only correct for files that have
been opened 'read only'. After writing to a file you need to respond
to an error from (f)close (unless the file contents is dispensable).
But it's too late to attempt error reporting in the destructor
(throwing exceptions from destructors is not an option in C++).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top