I've been trying to track down an intermittent crashing bug in my code (which uses setjmp), and narrowed it down to: shows up when compiling with /O2, goes away with /O2 /Oy-, i.e. only shows up with omit frame pointer.
http://msdn.microsoft.com/en-us/library/2kxx5t2c(v=vs.80).aspx suggests setjmp requires a frame pointer. Thus:
It seems that when a program that uses setjmp is compiled with /O2, Visual C++ silently generates code that causes intermittent stack corruption. Is this true, or am I missing something?
It seems to me that only the function that invokes setjmp should need to be compiled with frame pointer, the rest of the program - even the functions that call longjmp - should be okay to omit frame pointer. Is this true?
Edit: I've narrowed it down a little further.
Enabling frame pointer on the function that was calling setjmp made no difference, but that's because the compiler was doing that already, just as it should, obviously noticing it needed to be done, and doing it automatically.
What did make a difference was enabling frame pointer on main. That's not as bizarre as it sounds, since the crash was manifesting in return from main. Now that I think of it, all the examples I can find in a quick google search of setjmp usage, do it in main. Perhaps it happened that the Microsoft compiler team only tested it that way.
That being the idiomatic way to use it, perhaps the best workaround would be for me to just inline the setjmp-using function into main.
setjmp
/longjmp
that fails when there's no frame pointer. It seems like you'd have to go out of your way to make them this broken. In any case I'd just enable the frame pointer globally; it doesn't make much of a difference to performance.