How to debug 'value of ESP was not saved across function call' error?

十年热恋 提交于 2019-12-24 05:12:16

问题


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?


回答1:


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.




回答2:


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!




回答3:


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.



来源:https://stackoverflow.com/questions/1588594/how-to-debug-value-of-esp-was-not-saved-across-function-call-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!