To allow a legacy Fortran program to use a third-party DLL, we wrote a C++ wrapper.  The C++ files are compiled with g++,  Fortran files with GFortran, and the whole collection linked with g++.  I'm using MinGW32 4.5.0, installed using MinGW-Get version 0.1-alpha-5, on Windows XP.

After some struggles, it worked.  The main issue, of course, was to resolve all the mangled names and to be careful with string parameters.  (Actually, as of now I've eliminated all string parameters.)

However, when I add calls to the C++ functions from within deeply nested nested Fortran do-loops, I run into problems with the Fortran code and/or data space getting corrupted after many iterations.  This happens whenever the C++ functions are called, even if they do nothing other than return.  Weird things happen like variables change value when they shouldn't,  the loops don't stop when they should, "write" and "print" statements trigger a SIGSEGV, and so on.  (I can tell from running it in a debugger.)   If I comment out the C++ calls that do nothing, the errors don't occur.  Therefore, it would seem to be something to do with setting up the stack frame to make the call.  

The g++ compilations use the following options:
-mtune=generic -c -g 

The GFortran compilation uses
-g -c -fbounds-check 
-finit-local-zero -ffpe-trap=zero,invalid,underflow,overflow

The final linking line uses  g++
 -mtune=generic -static
-g -o 

Might anyone have some ideas about what it going on, and how to fix it?

Richard U