Tiza said:
Can anybody explain to me why this error occurs (during execution in
MVC++): [...]
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.
This is a bit outside the scope of plain C, but rather an implementation
detail. There are different ways to call a function. These mainly differ
in how things are stored on the stack and by whom, i.e. last argument
first vs last argument last, caller cleans stack vs callee cleans stack,
things that are passed in registers(ESP is one such register). This is
called the 'calling convention' of a function.
Under win32, different calling conventions are in use, which you can
specify with prefixes to your function declarations or compiler settings.
If you call one function with the compiler believing it was convention X
but implement the function with convention Y, you get above error (if
you're lucky!).
So, double check these:
- when mixing C and C++, make sure all plain C functions are declared
'extern "C"' for the C++ compiler (and only for that compiler make sure
via 'defined(__cplusplus)' and the preprocessor.
- when compiling, make sure the implementation file also includes the
header where the functions are declared, so you can't have any divergences
between the two. This is a good idea anyways.
- with your compiler, you can select the default calling convention which
is used for functions not otherwise marked. Make sure these match between
different compilation units.
- when using function pointers, the calling convention is in fact part of
the type of that function.
In case you have further questions about your particular calling
conventions or your compiler, please first consult the documentation and
then ask in a group dedicated to your compiler. While the general problem
is affecting every implementation of C, its details are still not suitable
for discussion here.
cheers
Uli