What do you use RAII for?

Discussion in 'C++' started by dean, Mar 27, 2008.

  1. dean

    dean Guest

    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
    dean, Mar 27, 2008
    #1
    1. Advertising

  2. dean wrote:

    > 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
    Paul Brettschneider, Mar 27, 2008
    #2
    1. Advertising

  3. In article <>,
    dean <> wrote:
    >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
    Yannick Tremblay, Mar 27, 2008
    #3
  4. dean wrote:

    > 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
    Michael Oswald, Mar 27, 2008
    #4
  5. dean

    James Kanze Guest

    On Mar 27, 3:11 am, dean <> wrote:

    > 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.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Mar 27, 2008
    #5
  6. On 2008-03-27 03:11, dean wrote:
    > 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.

    --
    Erik Wikström
    Erik Wikström, Mar 27, 2008
    #6
  7. dean

    Jon Harrop Guest

    dean wrote:
    > 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.

    --
    Dr Jon D Harrop, Flying Frog Consultancy Ltd.
    http://www.ffconsultancy.com/products/?u
    Jon Harrop, Mar 28, 2008
    #7
  8. On Wed, 26 Mar 2008 19:11:32 -0700 (PDT), dean wrote:
    >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++).


    --
    Roland Pibinger
    "The best software is simple, elegant, and full of drama" - Grady Booch
    Roland Pibinger, Mar 28, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MikeB
    Replies:
    4
    Views:
    340
    Owen Jacobson
    Oct 26, 2004
  2. Johannes Schaub (litb)

    Re: Why is RAII called RAII?

    Johannes Schaub (litb), Sep 12, 2010, in forum: C++
    Replies:
    2
    Views:
    388
    James Kanze
    Sep 18, 2010
  3. cpp4ever

    Re: Why is RAII called RAII?

    cpp4ever, Sep 12, 2010, in forum: C++
    Replies:
    1
    Views:
    395
    BGB / cr88192
    Sep 13, 2010
  4. Goran Pusic

    Re: Why is RAII called RAII?

    Goran Pusic, Sep 13, 2010, in forum: C++
    Replies:
    11
    Views:
    538
    ptyxs
    Sep 16, 2010
  5. Stefan Ram

    Where is RAII when you need it?

    Stefan Ram, Nov 17, 2013, in forum: C++
    Replies:
    33
    Views:
    372
    Alf P. Steinbach
    Nov 19, 2013
Loading...

Share This Page