473,466 Members | 4,869 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

RAII / handling failures during destruction - advice required

Hi,

Recently I was asked to look at some code where RAII is used to ensure
automatic cleanup of a resource. Unfortunately, cleaning up the resource
requires that the destructor make a call to an API which can (albeit under
dire circumstances) fail. As it stands, in the presence of a failed call to
the API, the destructor does nothing more than record the event in the
system log.

I'm uncomfortable with the fact that code further up the stack is unaware of
the failure but I'm also aware of the issues surrounding the throwing /
propagation of exceptions from destructors.

In view of this, I'm left wondering whether or not RAII is acceptable as a
means of managing this type of resource.

I'd appreciate others views on this.

Thanks in anticipation.

MikeB


Jul 22 '05 #1
4 1230
* MikeB:

Recently I was asked to look at some code where RAII is used to ensure
automatic cleanup of a resource. Unfortunately, cleaning up the resource
requires that the destructor make a call to an API which can (albeit under
dire circumstances) fail. As it stands, in the presence of a failed call to
the API, the destructor does nothing more than record the event in the
system log.

I'm uncomfortable with the fact that code further up the stack is unaware of
the failure but I'm also aware of the issues surrounding the throwing /
propagation of exceptions from destructors.

In view of this, I'm left wondering whether or not RAII is acceptable as a
means of managing this type of resource.


Do whatever is appropriate.

I.e., does the failed API call have an effect, and if so at what level
(thread, process, system, network), and who (computerwise, userwise)
should do something about that, if anything?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2

"Alf P. Steinbach" <al***@start.no> wrote in message
news:41****************@news.individual.net...
Do whatever is appropriate.

I.e., does the failed API call have an effect, and if so at what level
(thread, process, system, network), and who (computerwise, userwise)
should do something about that, if anything?


I'm not sure that the details are that relevant, but for the sake of
completeness, the failure of the API indicates that a lock which was at some
point acquired could not, for whatever reason, be released. Furthermore, the
class which manages the lock is a component from a 'generic' library, so in
the great tradition of error handling, I'm not convinced that it can know
how to 'do whatever is appropriate'.

I'd appreciate your input on how to 'do whatever is appropriate' in a
flexible manner.

Rgds,

MikeB


Jul 22 '05 #3
* MikeB:

"Alf P. Steinbach" <al***@start.no> wrote in message
news:41****************@news.individual.net...
Do whatever is appropriate.

I.e., does the failed API call have an effect, and if so at what level
(thread, process, system, network), and who (computerwise, userwise)
should do something about that, if anything?


I'm not sure that the details are that relevant, but for the sake of
completeness, the failure of the API indicates that a lock which was at some
point acquired could not, for whatever reason, be released. Furthermore, the
class which manages the lock is a component from a 'generic' library, so in
the great tradition of error handling, I'm not convinced that it can know
how to 'do whatever is appropriate'.

I'd appreciate your input on how to 'do whatever is appropriate' in a
flexible manner.


If it doesn't have any measurable effect, just log it (from the program)
and report it in whatever error reporting system is used (e.g. Bugzilla).

Otherwise if it can be easily dealt with at some level (e.g. terminating a
tread, process, system), do that also.

Otherwise leave it to the user to decide.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #4
On Sun, 24 Oct 2004 22:33:16 +0000, Alf P. Steinbach wrote:
* MikeB:

"Alf P. Steinbach" <al***@start.no> wrote in message
news:41****************@news.individual.net...
> Do whatever is appropriate.
>
> I.e., does the failed API call have an effect, and if so at what level
> (thread, process, system, network), and who (computerwise, userwise)
> should do something about that, if anything?


I'm not sure that the details are that relevant, but for the sake of
completeness, the failure of the API indicates that a lock which was at some
point acquired could not, for whatever reason, be released. Furthermore, the
class which manages the lock is a component from a 'generic' library, so in
the great tradition of error handling, I'm not convinced that it can know
how to 'do whatever is appropriate'.

I'd appreciate your input on how to 'do whatever is appropriate' in a
flexible manner.


If it doesn't have any measurable effect, just log it (from the program)
and report it in whatever error reporting system is used (e.g. Bugzilla).

Otherwise if it can be easily dealt with at some level (e.g. terminating a
tread, process, system), do that also.

Otherwise leave it to the user to decide.


To add to Alf Steinbach's good advice, I'd be inclined to test that the
lock is actually released using an assertion. The general case is that,
if you can acquire the lock, you should always be able to release it, and
if you can't there's likely a problem somewhere more fundamental than your
code.

So, if you're working with a "debug" version with assertions enabled,
it'll let you know when it happens, and at runtime a "mostly useless"
check goes away.

I'd be inclined to implement that in the header file, so that clients of
your library can enable and disable assertions themselves, even if the
rest of the library implementation is in the actual sources.

Owen

--
Some say the Wired doesn't have political borders like the real world,
but there are far too many nonsense-spouting anarchists or idiots who
think that pranks are a revolution.

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Pierre Rouleau | last post by:
As much as I love Python, I sometimes find myself wishing Python supported the RAII idiom (resource acquisition is initialization) that is available in C++, the emerging D language, and others. ...
26
by: codymanix | last post by:
Last night I had several thought about RAII and want to discuss a bit. Why doesn't CSharp support destructors in structs? Wouldn't that make RAII possible like in C++? When the struct goes out of...
23
by: Markus Elfring | last post by:
The class "auto_ptr" implements the RAII pattern for pointer types. It seems that an implementation is not provided for non-pointer values by the STL so far. I imagine to use the "acquisition" for...
4
by: Troy | last post by:
We recently installed the .Net framework on a windows 2000 server. Shortly after that we experienced intermitant problems running a web based program that accesses an Access 2002 database. The...
14
by: Mr Newbie | last post by:
I am often in the situation where I want to act on the result of a function, but a simple boolean is not enough. For example, I may have a function called isAuthorised ( User, Action ) as ?????...
9
by: plahey | last post by:
I have been dabbling in Python for a while now. One of the things that really appeals to me is that I can seem to be able to use C++-style RAII idioms to deal with resource management issues. ...
5
by: Kenneth Porter | last post by:
I've read this article and have some followup questions. http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thr ead/9d5324ce02f4d89b/ I'm working on an embedded robotics...
35
by: jeffc226 | last post by:
I'm interested in an idiom for handling errors in functions without using traditional nested ifs, because I think that can be very awkward and difficult to maintain, when the number of error checks...
9
by: Chad | last post by:
This might be a bit vague and poorly worded..... In my program, I handle function failures using fprintf() and exit() like: fprintf(stderr, "malloc failed"); exit(EXIT_FAILURE); There...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.