Note that there are some explanatory texts on larger screens.

plurals
  1. PODoes this code contain a hidden bug?
    primarykey
    data
    text
    <p>The following code :</p> <ul> <li>Runs fine when compiled with gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5/32bits)</li> <li>Runs fine when compiled with MSVC10 (Win7/32bits)</li> <li>Crashes when running with gcc version 4.5.2 (MinGW on Win7/32bits)</li> </ul> <p><strong>main.cpp</strong> :</p> <pre><code># include &lt;iostream&gt; # include &lt;csetjmp&gt; # include &lt;stdexcept&gt; using namespace std ; void do_work(jmp_buf context) { try { throw runtime_error("Ouch !") ; } catch(exception &amp; 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 &amp; e) { cout &lt;&lt; "Caught an exception saying : " &lt;&lt; e.what() &lt;&lt; endl ; } } </code></pre> <p>I tried debugging it but the program behaves strangely. Sometimes I could get past the first breakpoint (BP1), then crash at BP2, and sometimes control never reachs BP1, like if the program is stuck in an infinite loop. I cannot say more with my debugging skills. </p> <p>This code is the minimal I could get that exhibits the strange behavior with MinGW 4.5. I also noticed that :</p> <ul> <li>If I replace the <code>do_work</code> function call by its content, the program runs fine.</li> <li>If I remove the <code>try{ ... } catch(...){ }</code> block inside <code>do_work</code>, the program runs fine.</li> <li>Optimization flags have no effect (always crashing).</li> </ul> <p>I'm aware of the <code>setjmp/longjmp</code> issues in C++ code, but I'm forced to use it to interface with some legacy C code.</p> <p>My question :</p> <ul> <li>Is this a faulty/buggy/erroneous code ? Or is MinGW 4.5 mishandling the code ? (It is harsh and presumptuous to blame the tool, but I suspect some settings in it).</li> </ul> <p>Thanks for any advice.</p> <p><sub>Please retag if necessary.</sub></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload