views:

93

answers:

2

On rare occasions when my program exits, I get a "value of ESP has not been saved across a function call" error. The error is quite random and hard to reproduce.

How do I debug this error (VC++ 2008)? How harsh it is, as it only occurs on shutdown? Is the error visible also in release mode?

A: 

This means some part of your program wrote over the stack. That's bad. You're just lucky that right now it happens on shutdown, but sooner or later someone may use the failing function in another place.

When the message goes off you can see the function you're in. What you can do is rerun the program, and when entering the function, put a data breakpoint at the location esp was written to. Then run to the end of the function - the offending code will trigger the data breakpoint.

xtofl
+3  A: 

This means that either you call a function with a wrong calling convention - that often happens when you declare a function pointer improperly - or there's something overwriting the stack.

To debug the former check what function causes this situation. To debug the latter look for thing like stack-allocated buffer overruns.

sharptooth
It can also be caused by different parameters as well as wrong call convention - eg. if an application calls a DLL function with f(a), but the DLL is compiled with the signature f(a, b).
AshleysBrain
Or if someone decided to change the order of functions in a dll while the using application assumes the previous order, e.g. when breaking the rule of static interfaces for COM.
Georg Fritzsche