How to debug "ESP value was not saved on function call error?"

On the rare occasion when my program exits, I get the error "The ESP value was not caused by a function error." The error is quite random and difficult to reproduce.

How do I debug this error (VC ++ 2008)? How harsh is it since it only happens when you shut down? Is the error also visible in release mode?

+2


source to share


3 answers


This means that you are calling a function with the wrong calling convention - this often happens when you declare a function pointer incorrectly, or overwrite the stack with something.



To debug the previous one, check which function is causing this situation. To debug the latter, look for a thing like a stack-allocated buffer overflow.

+5


source


I had the same problem and managed to fix it. In my case, everything was very specific. It's hard to tell without some code sample. This was causing the problem. Below I will 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 because it left 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 direct declaration and just included the header file.

#include "MyClass.h"
typedef (MyClass::*CallBack)(Object*);

      

To summarize, don't forward the declaration when you want to use a member function pointer from that class!

+1


source


This means that part of your program is written above the stack. This is bad. You are just lucky that this is happening on shutdown right now, but sooner or later someone might be using the crash function elsewhere.

When the message is muted, you can see the function you are in. What you can do is restart the program, and when you enter the function, place a data breakpoint where it was written esp

. Then run to the end of the function - the offending code will trigger a data breakpoint.

0


source







All Articles