How can I stick to RAII?

Discussion in 'C++' started by Michael Tsang, Apr 19, 2010.

  1. In C++, everything should be acquired and initialised in constructors and
    released in destructors but I'm in a trouble trying to do this. Nearly all
    code written by me requires interacting with C libraries, most commonly
    POSIX so I am forced to scatter my code with open(), close(), opendir(),
    closedir(), fdopen(), pipe(), mmap(), etc inside middle of functions and it
    is very difficult to get it right when the code throws exceptions. I know
    that RAII is the only way to prevent leaking non-memory resources such as
    file handles.
    Michael Tsang, Apr 19, 2010
    #1
    1. Advertising

  2. Leigh Johnston wrote:

    >
    >
    > "Michael Tsang" <> wrote in message
    > news:hqhn3a$4kc$-september.org...
    >> In C++, everything should be acquired and initialised in constructors and
    >> released in destructors but I'm in a trouble trying to do this. Nearly
    >> all code written by me requires interacting with C libraries, most
    >> commonly POSIX so I am forced to scatter my code with open(), close(),
    >> opendir(), closedir(), fdopen(), pipe(), mmap(), etc inside middle of
    >> functions and it
    >> is very difficult to get it right when the code throws exceptions. I know
    >> that RAII is the only way to prevent leaking non-memory resources such as
    >> file handles.

    >
    > Interacting with C libraries shouldn't be an issue when using RAII, e.g.
    >
    > class foo
    > {
    > private:
    > struct handle
    > {
    > handle() { }
    > ~handle() { close(...); }
    > };
    > public:
    > foo(...) : iHandle(open(...)) {}
    > private:
    > handle iHandle;
    > };
    >
    > /Leigh


    How about when using calls such as dup2(), pipe(), fdopen(), etc?
    Michael Tsang, Apr 19, 2010
    #2
    1. Advertising

  3. Keith H Duggar, Apr 19, 2010
    #3
  4. Michael Tsang

    Jorgen Grahn Guest

    On Thu, 2010-04-22, Yannick Tremblay wrote:
    > In article <>,
    > Leigh Johnston <> wrote:
    >>
    >>
    >>"Keith H Duggar" <> wrote in message
    >>news:...
    >>> ScoreGuard:
    >>>
    >>> http://www.drdobbs.com/cpp/184403758;jsessionid=MOMHNDVPM04G5QE1GHOSKH4ATMY32JVN
    >>>
    >>> and related ideas. You can write little wrapper functions of all sorts
    >>> and construct ScopeGuards of them. You can also generalize it a bit
    >>> and do without the *Obj* variants by simplifying ScopeGuard and
    >>> combining it with Don Clugston's FastDelegates:
    >>>
    >>> http://www.codeproject.com/kb/cpp/FastDelegate.aspx
    >>>
    >>> KHD

    >>
    >>Whilst I agree that ScopeGuards can be useful it is important to note
    >>however that their overuse may be an indication of a lack of class based
    >>abstractions (I am primarily an OO programmer, not a procedural programmer).

    >
    > But the OP query relates to interacting with libc functions like
    > open(), fopen(), popen(), pipe(), etc. ScopeGuards are pretty close
    > to ideal to make it easy to write exception safe C++ code that
    > interact with lower level C libraries.


    But if you're wrapping a C library, I guess you can often find some
    abstraction level where it makes sense to introduce a class, and then
    you use such objects instead of thinly wrapped (in this case) file
    descriptors. I assume that's what Leigh meant.

    For example, I think it's generally better to wrap pipe() in some way
    suited to your specific application, rather than to just fix the
    possible resource leak.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Apr 22, 2010
    #4
    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. bussiere maillist
    Replies:
    0
    Views:
    486
    bussiere maillist
    Dec 7, 2007
  2. Tim Chase
    Replies:
    0
    Views:
    575
    Tim Chase
    Dec 7, 2007
  3. Johannes Schaub (litb)

    Re: Why is RAII called RAII?

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

    Re: Why is RAII called RAII?

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

    Re: Why is RAII called RAII?

    Goran Pusic, Sep 13, 2010, in forum: C++
    Replies:
    11
    Views:
    550
    ptyxs
    Sep 16, 2010
Loading...

Share This Page