Reputation: 1587
My x64 application is compiled with /EHa,
I want to guard my code against access violations, but the application process seems to crash regardless of my SEH and termination handlers.
void my_se_translator(unsigned int, struct _EXCEPTION_POINTERS*) {
cout << "se_translator_called" << endl;
}
void my_terminate_handler() {
cout << "my_terminate_handler_called" << endl;
}
#pragma optimize("", off)
void test() {
int* k = nullptr;
int z = *k;
}
#pragma optimize("", on)
int main(int argc, char* argv[]) {
_set_se_translator(my_se_translator);
set_terminate(my_terminate_handler);
test();
return 0;
}
Would appreciate a good explanation for why _set_se_translator does not work in this case?
Upvotes: 2
Views: 2063
Reputation: 121
From the MSDN website:
Your translator function should do no more than throw a C++ typed exception. If it does anything in addition to throwing (such as writing to a log file, for example) your program might not behave as expected, because the number of times the translator function is invoked is platform-dependent.
when you add a simple exception class:
class myException{
};
Then throw the exception in the my_se_translator function, and catch the exception in either the main or test function your example should work.
void my_se_translator(unsigned int, struct _EXCEPTION_POINTERS*) {
std::cout << "se_translator_called" << std::endl;
throw myException();
}
int main(int argc, char* argv[]) {
_set_se_translator(my_se_translator);
set_terminate(my_terminate_handler);
try{
test();
}
catch(myException e) {
std::cout << "EXCEPTION!" << std::endl;
}
return 0;
}
Upvotes: 2