The following code:
- Works well when compiling with gcc version 4.4.5 (Ubuntu / Linaro 4.4.4-14ubuntu5 / 32bits)
- Works well when compiling with MSVC10 (Win7 / 32bits)
- Failure to work with gcc version 4.5.2 (MinGW on Win7 / 32bits)
main.cpp :
# include <iostream> # include <csetjmp> # include <stdexcept> using namespace std ; void do_work(jmp_buf context) { try { throw runtime_error("Ouch !") ; } catch(exception & e) { } longjmp(context, -1) ; //BP1 } int main(int, char *[]) { jmp_buf context ; try { if( setjmp(context) != 0 ) { throw runtime_error("Oops !") ; //BP2 } do_work(context) ; } catch(exception & e) { cout << "Caught an exception saying : " << e.what() << endl ; } }
I tried debugging it, but the program is behaving strangely. Sometimes I could go past the first breakpoint (BP1), then crash at BP2, and sometimes the control never reaches BP1, for example, if the program gets stuck in an infinite loop. I can not say more with my debugging skills.
This code is the smallest I could get by exhibiting strange behavior with MinGW 4.5. I also noticed that:
- If I replace the call to the
do_work function do_work its contents, the program will work fine. - If I delete the
try{ ... } catch(...){ } do_work inside do_work , the program works fine. - Optimization flags do not affect (always crash).
I am aware of the problems of setjmp/longjmp in C ++ code, but I have to use it to interact with some legacy C code.
My question is:
- Is this an erroneous / erroneous / erroneous code? Or does MinGW 4.5 process code incorrectly? (Itโs tough and presumptuous to blame the tool, but I suspect that it has some settings).
Thanks for any advice.
Please try if necessary.
source share