473,399 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

DLL Hell - gracefull handling/app termination

Hi,

I am writing a Win32 DLL. I want to be able to handle any SEGVs
(segmentation violations) gracefully, by using an error handler of sorts.

Currently, if a user of my DLL (typically a VB programmer) passes a null
(or invalid) pointer to my library - the entire application crashes,
leaving shared memory, database connections etc in a "dirty" state. I
would like a way of gracefully handling user "actions" like this -
without crashing spectacularly.

Any help much appreciated.

Dec 20 '05 #1
2 1247
"Susan Baker" <sb****@no.spam.net> wrote in message
news:do*********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com...
I am writing a Win32 DLL. I want to be able to handle any SEGVs
(segmentation violations) gracefully, by using an error handler of sorts.

Currently, if a user of my DLL (typically a VB programmer) passes a null
(or invalid) pointer to my library - the entire application crashes,
leaving shared memory, database connections etc in a "dirty" state. I
would like a way of gracefully handling user "actions" like this - without
crashing spectacularly.

Any help much appreciated.


This is tricky business.

On the one hand one make the case that case that arguments past from those
who are prone to error should be checked. So, faced with a pointer p, one
might write:

if ( p == 0 )
// do some error / exception handling

else
{
// do some more checks
}

Now you know it is not null pointer that you are dealing with. But it might
be a "wild" pointer. So, one can make the case that pointers should be
checked with IsBadStringPtr() or one of its cousins. (Note that these
functions are _slow_).

If that check passes you know the pointer is good. Well, maybe. It is
possible that the pointer is valid, but the length it uses extends past the
length of the object to which it points and "overflows" into whatever
happens to be adjacent in memory - ( and heaven help us if the adjacent
locations are on the stack).

You could design your objects such that their first data member has a size
indicator and force your callers to set a size it.

But then how do you know that they set it properly? So, where do you draw
the line? How many tests do you make and how can you be sure bad things
won't happen?

You can use what is called structured exception handling

http://msdn.microsoft.com/library/de...n_handling.asp

to detect such things. With it you write "guarded" sections a filter and an
exception block

// Guarded section

__try
{
}

__except ( /* simple filter */ EXCEPTION_EXCEUTE_HANDLER )
{
// exception block cleans up
}

You try the iffy operation in the guarded section.

If something bad happens control passes to the filter. It returns an
indication of what to do next. Above, I unconditionally pass control to the
exception handler.

Then the exception handler tries to recover.

It sounds promising, and it is, but it is not a cure-all. That's because by
the time something really bad has happened your application's state may be
trashed to such an extent that continuing only makes things worse.

One strategy is to report the error, print a crash dump with
MiniDumpWriteDump() and find the guilty developer and chain him/her to the
desk until the bug is fixed.

Note that I am making the assumption that your application is more C than
C++ because you permit VB callers. If I am wrong there are another set of
issues to deal with.

In short, there really is no defense against bad programming. Sadly
applications can and will crash when written sloppily. I've sketched one way
to address the problem along the lines of your question.

Better solutions are at the language and environment level. Some would say
that in modern C++ there should be no "naked" pointers. Of course that's a
problem if your clients are written in VB. And the .Net platform tries to
hide pointers as much as possible.

Regards,
Will


Dec 20 '05 #2


William DePalo [MVP VC++] wrote:
"Susan Baker" <sb****@no.spam.net> wrote in message
news:do*********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com...
I am writing a Win32 DLL. I want to be able to handle any SEGVs
(segmentation violations) gracefully, by using an error handler of sorts.

Currently, if a user of my DLL (typically a VB programmer) passes a null
(or invalid) pointer to my library - the entire application crashes,
leaving shared memory, database connections etc in a "dirty" state. I
would like a way of gracefully handling user "actions" like this - without
crashing spectacularly.

Any help much appreciated.

This is tricky business.

On the one hand one make the case that case that arguments past from those
who are prone to error should be checked. So, faced with a pointer p, one
might write:

if ( p == 0 )
// do some error / exception handling

else
{
// do some more checks
}

Now you know it is not null pointer that you are dealing with. But it might
be a "wild" pointer. So, one can make the case that pointers should be
checked with IsBadStringPtr() or one of its cousins. (Note that these
functions are _slow_).

If that check passes you know the pointer is good. Well, maybe. It is
possible that the pointer is valid, but the length it uses extends past the
length of the object to which it points and "overflows" into whatever
happens to be adjacent in memory - ( and heaven help us if the adjacent
locations are on the stack).

You could design your objects such that their first data member has a size
indicator and force your callers to set a size it.

But then how do you know that they set it properly? So, where do you draw
the line? How many tests do you make and how can you be sure bad things
won't happen?

You can use what is called structured exception handling

http://msdn.microsoft.com/library/de...n_handling.asp

to detect such things. With it you write "guarded" sections a filter and an
exception block

// Guarded section

__try
{
}

__except ( /* simple filter */ EXCEPTION_EXCEUTE_HANDLER )
{
// exception block cleans up
}

You try the iffy operation in the guarded section.

If something bad happens control passes to the filter. It returns an
indication of what to do next. Above, I unconditionally pass control to the
exception handler.

Then the exception handler tries to recover.

It sounds promising, and it is, but it is not a cure-all. That's because by
the time something really bad has happened your application's state may be
trashed to such an extent that continuing only makes things worse.

One strategy is to report the error, print a crash dump with
MiniDumpWriteDump() and find the guilty developer and chain him/her to the
desk until the bug is fixed.

Note that I am making the assumption that your application is more C than
C++ because you permit VB callers. If I am wrong there are another set of
issues to deal with.

In short, there really is no defense against bad programming. Sadly
applications can and will crash when written sloppily. I've sketched one way
to address the problem along the lines of your question.

Better solutions are at the language and environment level. Some would say
that in modern C++ there should be no "naked" pointers. Of course that's a
problem if your clients are written in VB. And the .Net platform tries to
hide pointers as much as possible.

Regards,
Will


Very informative post.

Many thanks.

Regards,

Sue

Dec 20 '05 #3

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

Similar topics

9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
12
by: Ritz, Bruno | last post by:
hi in java i found that when a method has a throws clause in the definition, callers must either handle the exceptions thrown by the method they are calling or "forward" the exception to the...
3
by: mydejamail | last post by:
Is there a major flaw with PHPs variable handling? I am writing some PHP scripts and the variables seem to be all over the place. Eg in one instance, I am counting the number of times a for loop...
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 ?????...
3
by: Susan Baker | last post by:
Hi, I am writing a (unmanaged) Win32 DLL. I want to be able to handle any SEGVs (segmentation violations) gracefully, by using an error handler of sorts. Currently, if a user of my DLL...
10
by: Anthony England | last post by:
(sorry for the likely repost, but it is still not showing on my news server and after that much typing, I don't want to lose it) I am considering general error handling routines and have...
13
by: junw2000 | last post by:
Is C++ Exception handling useful? think it is too complicated. What kinds of project need to use it? Thanks.
94
by: Chad | last post by:
On to top of page 163 in the book "The C Programming Langauge" by K & R, they have the following: char *strdup(char *s) { char *p; p=(char *)malloc(strlen(s)+1); if( p != NULL) strcpy(p,s):...
0
by: Tim Golden | last post by:
Robert Rawlins wrote: You want to look at the atexit module: http://docs.python.org/lib/module-atexit.html but mind the caveats: """ Note: the functions registered via this module are not
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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,...

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.