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?
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.
I had this same problem and managed to fix it. In my case though things were are very specific. It's hard to tell without some sample code posted. This is what was causing the problem. Below I'll show an example of what was breaking my program.
class MyClass; //Forward declaration
typedef (MyClass::*CallBack)(Object*);
When registering a new CallBack the program crashed as it was leaving the current function call.
class ThisClass : public MyClass
{
//...
}
//...
//...
void ThisClass::Init(void)
{
Sys.RegisterCallBack((CallBack)&ThisClass::Foo);
} //The program crashed at this line
To fix the problem I got rid of the forward declaration and simply included the header file.
#include "MyClass.h"
typedef (MyClass::*CallBack)(Object*);
To summarize, do not forward declare when you want to use a member function pointer from that class!
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.