Strange stack variable corruption error after calling extern(C) function

Lewis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Apr 14 17:23:42 PDT 2017


On Tuesday, 3 May 2016 at 19:06:30 UTC, cc wrote:
> I see, thanks.  Double checking the original headers, I see the 
> function is defined as:
>
> FMOD_RESULT F_API FMOD_System_CreateSound               
> (FMOD_SYSTEM *system, const char *name_or_data, FMOD_MODE mode, 
> FMOD_CREATESOUNDEXINFO *exinfo, FMOD_SOUND **sound);
>
>
> F_API seems to be defined as follows:
>
> ...
> #elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) 
> || defined(_WIN64)
>     #define F_STDCALL _stdcall
>     #define F_DECLSPEC __declspec
>     #define F_DLLEXPORT ( dllexport )
> ...
> #ifdef DLL_EXPORTS
>     #define F_API F_DECLSPEC F_DLLEXPORT F_STDCALL
> #else
>     #define F_API F_STDCALL
> #endif
>
>
> I tried going by the information suggested on this page: 
> http://wiki.dlang.org/D_binding_for_C#cdecl.2C_pascal.2C_stdcall
> but if I declare the function, in D, as:
> extern(Windows) FMOD_RESULT FMOD_System_CreateSound
>   (FMOD_SYSTEM *system, const char *name_or_data, FMOD_MODE 
> mode, FMOD_CREATESOUNDEXINFO *exinfo, FMOD_SOUND **sound);
>
> it fails to link with "Error 42: Symbol Undefined 
> _FMOD_System_CreateSound at 20".  With extern(C) it compiles and 
> runs but the problem from above persists.

Holy crap, thank you. I know this is late, but I was playing 
around with derelictFMOD, and ran into a strange crash like yours 
on shutdown. Looking at the disassembly revealed that 
FMOD_System_Close() was popping more off the stack as it finished 
than I would have expected it to. I tried changing the calling 
convention in derelictFMOD to extern(Windows) as mentioned here, 
and that fixed the problem.

I'll submit a pull request to that project to get that fixed. But 
thank you both for having this forum discussion 11 months prior 
that would eventually tip me in the direction of the solution  :)


More information about the Digitalmars-d-learn mailing list