Catching a hot potato
Steven Schveighoffer
schveiguy at yahoo.com
Mon Oct 17 05:49:15 PDT 2011
On Sat, 15 Oct 2011 08:18:59 -0400, Gor Gyolchanyan
<gor.f.gyolchanyan at gmail.com> wrote:
> Thanks for the detailed answer.
> Example of when i would like to recover from a segfault:
> I have different independent parts of an app, which MAY cooperate. If,
> however, one of them crashes (e.g. they come from different sources)
> the other one will continue to work but without the cooperation
> functionality.
A segfault means something has attempted reading/writing to a memory
location that was not allocated. Note that this does *not* necessarily
mean dereferencing null.
The worst case scenario is that something has invalidly written to
*allocated* memory before your program reads from/writes to unallocated
memory. Usually this is a result of the usage of the now-corrupted
allocated memory.
For instance, a module that caused the segfault might *not* be the module
which misbehaved! It could have been a rogue third module which corrupted
the memory in the segfaulting module. Closing the segfaulting module does
no good.
Catching a segfault and continuing the program is a very bad idea. You
have no idea what valid memory was corrupted before the segfault occurred,
or who did it. The only logical thing to do is exit the program, maybe
attempting to print a stack trace (which might fail if the stack is
corrupt).
-Steve
More information about the Digitalmars-d
mailing list