我正在尝试使用sigaction设置异常处理程序。它适用于第一个例外。但是在第一个异常之后没有调用sigaction处理程序,并且当第二个信号发生时程序突然结束。
#include <iostream>
#include <signal.h>
#include <exception>
#include <string.h>
typedef void (*SigactionHandlerPointer)(int iSignal, siginfo_t * psSiginfo, void * psContext);
using namespace std;
void SigactionHookHandler( int iSignal, siginfo_t * psSiginfo, void * psContext )
{
cout << "Signal Handler Exception Caught: std::exception -- signal : " << iSignal << " from SigactionHookHandler()" << endl;
throw std::exception();
}
class A
{
public:
A() {}
virtual ~A() {}
virtual void fnct1();
virtual void fnct2() { fnct3(); }
virtual void fnct3() { fnct4(); }
virtual void fnct4();
};
void
A::fnct1()
{
try {
fnct2();
}
catch( std::exception &ex ) {
cerr << "Signal Handler Exception Caught" << endl;
}
catch (...)
{
cerr << "Unknow Exception Caught: " << endl;
}
}
void
A::fnct4()
{
*(int *) 0 = 0; // Access violation
}
int main()
{
struct sigaction oNewSigAction;
struct sigaction oOldSigAction;
memset(&oNewSigAction, 0, sizeof oNewSigAction);
oNewSigAction.sa_sigaction = SigactionHookHandler;
oNewSigAction.sa_flags = SA_SIGINFO;
int iResult = sigaction( SIGSEGV, &oNewSigAction, &oOldSigAction );
cout << "sigaction installed handler with status " << iResult << endl;
A * pA = new A();
cout << "Next message expected is : <<Signal Handler Exception Caught: std::exception>> to pass this test" << endl;
pA->fnct1();
// Second exception will never be call the sigaction handler.
cout << "Next message expected is : <<Signal Handler Exception Caught: std::exception>> to pass this test" << endl;
pA->fnct1();
return 0;
}
答案 0 :(得分:8)
信号和例外彼此无关。您正在使用的(从异步信号处理程序抛出异常)只能在少数支持它的编译器之间移植,例如GCC和带有-fnon-call-exceptions
的Intel C / C ++。
那就是说,你忘了做的就是解锁信号:当信号处理程序正在执行时,相同信号的传递被阻止,当信号处理程序通过异常退出时,它不会被解除阻塞。按如下方式更改信号处理程序:
void SigactionHookHandler( int iSignal, siginfo_t * psSiginfo, void * psContext
{
cout << "Signal Handler Exception Caught: std::exception -- signal : " << iSignal << " from SigactionHookHandler()" << endl;
sigset_t x;
sigemptyset (&x);
sigaddset(&x, SIGSEGV);
sigprocmask(SIG_UNBLOCK, &x, NULL);
throw std::exception();
}
答案 1 :(得分:2)
标准C ++没有提及信号,也没有说明它们如何与异常交互。您要做的事情将完全针对您正在使用的OS平台以及您编译代码的特定编译器。