473,763 Members | 2,375 Online
Bytes | Software Development & Data Engineering Community
+ 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 1256
* 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.i ndividual.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.i ndividual.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.i ndividual.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
4759
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. In some situations (like controlling non-memory resources) it is nice to be able to create an object that will execute some code on its destruction. For example, an object that controls access to a critical section: the creation of the object...
26
448
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 scope, the dtor could be immediately be called (no GC needed). For that, you don't have to declare the whole File class as a struct (which would be not good for performance when File has a lot of data-members). Instead creating a thin wrapper...
23
2296
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 boolean values or flags. Would you like that a template class will be added to implement locks or state indicators for example? Regards, Markus
4
2859
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 intranet .asp program works, but as soon as it tries to access the database for normal users, it gives us an "unspecified error" and that it can't access the data base. As the administrator, I found my access was relatively stable. Anyone else...
14
1845
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 ????? OK, this function may return a boolean, and if this is true, then no message back is really required, but if it fails then some supporting message needs to be returned to the calling code. As I see it there are a few options.
9
3005
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. For those that have no idea what I am talking about (I learn a lot reading posts on subjects in which I am clueless), consider the following code snippet: for line in file(name): ...print line,
5
1838
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 application that cannot terminate. I often have the need to temporarily change some setting before performing an operation. If the operation fails, I still need to restore the original setting. It's possible for the restore operation to fail
35
3805
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 gets about 3 or so. It also gets very awkward in nested loops, where you want to check for normal loop processing in the loop condition, not errors. Yes, you could put some generic exit flag in the loop condition, but when you're simply done if...
9
2588
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 are 5 of these. Since each one has two lines, the total lines of
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10145
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9998
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7366
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3917
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 we have to send another system
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.