Just doing a bit of thinking recently about signals and c++.
With signals and c++ in general, receipt of a signal by default causes
the program to quit, bypassing all those nice destructors that you put
in there. A real curse, so my first thought was to munge signals into
some sort of exception, but interestingly enough this a supremely
difficult thing to achieve from inside c++. You cannot throw an
exception from inside the signal handler as it just doesn't work as
the exception mechanisn has no idea where you came from before the
signal so it just barfs and aborts you. You can ignore the signal and
that's fine. But if you want to exit you're in a bit of trouble. You
can feck around with setjmp longjmp stuff, but that fills me with
horror.
There is some suggestion of setting a global flag in the signal
handler and then testing this regularly and throw an exception from
normal code when this flag is seen, but thats just ridiculous, a test
after every function call ? Back to errno land and bye bye the point
of using exceptions in the first place.
Though this looks like the only possible approach, protect each call
that might trigger a signal such as SIGPIPE with a signal handler that
sets a flag, and then test for the flag after the possibly suspicious
function and then throw an exception. Basically synchronize the
signals manually. Though this isn't going to work for signals that are
likely to be delivered at utterly unpredictable times such as SIGINT.
What you'd really like to happen is to have those signals delivered to
your program as raised exceptions, all done for you by some external
mechanism, interestingly I see that the digital unix compiler can
convert many signals into exceptions itself, (see
http://www.unix.digital.com/cplus/docs/uguexcp.htm) The world would be
a better place if this mechanism became common across compilers, egcs
for example.
In the absence of such massive cleverness though I imagine that Im
just going to have to live with the ugliness of signals crapping all
over the destructors of a program, unless anyone has any cunning
ideas.
C.
Maintained by the ILUG website team. The aim of Linux.ie is to
support and help commercial and private users of Linux in Ireland. You can
display ILUG news in your own webpages, read backend
information to find out how. Networking services kindly provided by HEAnet, server kindly donated by
Dell. Linux is a trademark of Linus Torvalds,
used with permission. No penguins were harmed in the production or maintenance
of this highly praised website. Looking for the
Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!