PDA

View Full Version : QLibrary::resolve() crash on return



gib
29th March 2010, 07:50
I am able to invoke a procedure in my DLL successfully, and all seems OK until the execution is complete. On return from the DLL procedure my Qt application crashes silently.

I am executing the DLL from within a thread.

void ExecThread::run()
{
...
QLibrary myLib(dll_path);

typedef int (*MyPrototype)(int *, char *, int, char *, int, char *, int, char *, int);
MyPrototype execute = (MyPrototype) myLib.resolve("EXECUTE");
if (execute) {
execute(...)
}
myLib.unload();
}

If I comment out the call to execute() the program doesn't crash. I suspect the problem is stack related. My DLL is in Fortran90, and there is a trick to passing strings from C to Fortran. My Fortran subroutine expects an integer (by reference) and four strings, each of which is passed as a pair of arguments, a C string and an int holding the string length. This works fine in Python (using windll.LoadLibrary()), and almost works here, since the subroutine executes correctly.

squidge
29th March 2010, 08:34
Sounds like the C routine was expecting the called function to fix up the stack, but the function was expecting the caller to fix up the stack. So the called function works correctly, but then your program will crash shortly afterwards, as the stack offset will be incorrect.

Have you tried changing the calling convention?

gib
29th March 2010, 08:52
I agree that it looks like an issue of who has responsibility for cleaning up the stack. I'm using STDCALL, which as I understand it is the correct calling convention for calling Fortran from C. To be precise this is what I'm using:

subroutine execute(ncpu,infile,outfile,resfile,runfile)
!DEC$ ATTRIBUTES DLLEXPORT :: EXECUTE
!DEC$ ATTRIBUTES STDCALL, REFERENCE, MIXED_STR_LEN_ARG, ALIAS:"EXECUTE" :: execute
integer :: ncpu
character*(*) :: infile,outfile,resfile,runfile

I confess to not really understanding all the ins and outs of calling conventions, and when in doubt I usually consult the experts at the Intel Fortran forum. I do need to provide them with the correct info, since they are not very familiar with C++, and less with Qt. Am I correct in thinking that I need the convention for calling from C?

gib
29th March 2010, 09:06
I've changed to:
!DEC$ ATTRIBUTES C, REFERENCE, MIXED_STR_LEN_ARG, ALIAS:"EXECUTE" :: execute
and now it works. This is a bit annoying since it means I need different DLLs for the C++ and the Python versions of my Qt application.
Thanks.