I’m writing some inline functions for fun and it throws an exception I have never encountered before. The funny thing is, that if I continue, after the exception just stopped the flow of execution of my program, it will return the sum of two integers.
__declspec(dllexport) int addintegers(int one, int two)
{
int answer = 0;
__asm
{
mov eax, 0
push two
push one
call add
mov answer, eax
}
return answer;
} // Debugger stops here with exception message
Exception Message:
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.
// add function definition
int add(int one, int two)
{
return one + two;
}
I don’t know much about assembler, and you don’t show us the declaration of
add(), but if it adheres to C’s calling convention you have to pop the arguments from the stack after the call returned to the caller.Requiring the caller to clean up the stack, rather than the callee, is what allows C to have functions with a variable number of arguments, like
printf().