This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Q: Catch a segmentation fault


Alexandre Oliva wrote:
> 
> On Jan 25, 2001, Roger Wernersson <roger.wernersson@mindark.com> wrote:
> 
> > the Win32-programmers put in some code to catch segmentation faults.
> 
> Segmentation faults generate signals on Unix, and signals aren't
> translated into C++ exceptions.  You'd have to install a signal
> handler, but it's not portable to throw from signal handlers either.
> It might work for you.

It wouldn't be a good idea even if it was known to work. Exceptions are
synchronous; signals are asynchronous. If an exception might arise at
any moment, instead of at well-defined places, all exception safety
guarantees would go out the window. In particular, if a signal raises an
exception while the main process is in a function that has an exception
specification that doesn't include that one (quite likely if the
function is part of someone else's library -- e.g. libstdc++), your
program will abort.

When I need signal-aware C++ code, I usually handle it by setting a flag
in the signal handler and polling the flag in the main code.

-- 
Ross Smith <ross.s@ihug.co.nz> The Internet Group, Auckland, New Zealand
========================================================================
          "Normally he was insane, but he had lucid moments
          when he was merely stupid."     -- Heinrich Heine

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]