Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How can I stick to RAII?

Reply
Thread Tools

How can I stick to RAII?

 
 
Michael Tsang
Guest
Posts: n/a
 
      04-19-2010
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.
 
Reply With Quote
 
 
 
 
Michael Tsang
Guest
Posts: n/a
 
      04-19-2010
Leigh Johnston wrote:

>
>
> "Michael Tsang" <> wrote in message
> news:hqhn3a$4kc$...
>> 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?
 
Reply With Quote
 
 
 
 
Keith H Duggar
Guest
Posts: n/a
 
      04-19-2010
ScoreGuard:

http://www.drdobbs.com/cpp/184403758...OSKH4ATMY32JVN

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
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      04-22-2010
On Thu, 2010-04-22, Yannick Tremblay wrote:
> In article <>,
> Leigh Johnston <> wrote:
>>
>>
>>"Keith H Duggar" <> wrote in message
>>news:18b3b1fe-8937-4bfe-9dfc-...
>>> ScoreGuard:
>>>
>>> http://www.drdobbs.com/cpp/184403758...OSKH4ATMY32JVN
>>>
>>> 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 .
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Differences between Sony Memory Stick & memory Stick Pro vs Memory Stick Duo? zxcvar Digital Photography 3 11-28-2004 10:48 PM
Memory stick pro is cheaper than normal memory stick fanfaron Digital Photography 2 06-07-2004 03:27 PM
Sony DSC-U30 Memory Stick vs. Memory Stick Pro Barry Lovelace Digital Photography 1 02-11-2004 09:23 PM
Sony Memory Stick Pro vs Standard Memory Stick jwv Digital Photography 13 07-19-2003 12:04 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57