\$40

### Image of the Day Submit

IOTD | Top Screenshots

## Error: ...value of ESP was not properly saved...

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1James Leighe  Members

Posted 17 January 2012 - 03:27 AM

(C++, Visual Studio 2010)

I get the following error when my app returns:

"Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention."

But only if two things are true:
1) You start the app outside of the IDE and then attach to it when it breaks.
2) You call a function implemented in a lib.

This code will reproduce the error:

int __stdcall
WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, char* lpCmdLine, int nCmdShow )
{
__debugbreak();
TestFn();

return 0;
}

(Code for lib not shown, but it would just implement 'TestFn()')

What's going on here?

edit:
Decided to show the code for the lib in case I'm doing that wrong:
test.h
bool TestFn( );


test.cpp

bool TestFn( )
{
return true;
}


### #2Hodgman  Moderators

Posted 17 January 2012 - 03:34 AM

This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention

Either:
1) TestFn contains code with a buffer-overrun/etc that is corrupting the stack.
2) ^^What the error says: a function was compiled with one calling convention, but was called using another convention.

If (2), it's probably caused by the lib having been compiled using different compiler settings than your program, so that your program and the lib both have a different idea of what the default convention should be.

What happens if you use CALLBACK instead of __stdcall on winmain?

### #3James Leighe  Members

Posted 17 January 2012 - 04:00 AM

No dice on using 'CALLBACK'.

It couldn't be (1) since the fn just returns true, and (2) would be strange since I created new projects for the test and used fully default settings.

(I of course switched it to compile the lib as a lib but that's all)

Remember, it only happens when you run the app outside the IDE and then break in when the __debugbreak hits. If I debug it from within the IDE it runs fine!

EDIT:

I think I tested similar code on another PC and it did not have the issue, prompting me to reinstall windows.

Still have the error however. Could somehow be something about my PC.

### #4James Leighe  Members

Posted 20 January 2012 - 11:37 AM

Just to update this, it IS something with my PC. The error will not happen on any other PC I try it on.

So it's some kind of hardware issue.

### #5mhagain  Members

Posted 21 January 2012 - 08:09 AM

Are you overclocked? All manner of weird and seemingly impossible things can happen with overclocking... http://blogs.msdn.com/b/oldnewthing/archive/2005/04/12/407562.aspx

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

### #6Matias Goldberg  Members

Posted 21 January 2012 - 09:03 AM

This usually happens when you're using an old DLL while linking against a new lib & header that was modified.
Most likely one of the DLLs your app is using in your PC is different (newer or older) and that's why you get the error.

From what it looks like your VC Studio debug CRT DLLs are miss-matched (or your release DLLs, which are installed from a redist like this one). Try reinstalling Visual Studio.