Pergunta

I am compiling a Fortran program on windows with visual studio 2012 and intel fortran 2013. In the program, I call a C function which is part of a static library:

call myfunction(arg1,arg2,...);

When I run in debug mode, everything is fine. When I run it in release mode, the program crashes during that function call (I test it by printing to the screen before/after the call) with the following error:

forrtl: severe (157): Program Exception - access violation

Now the interesting part, if I add print statements after and before the call as follows

print 'Calling myfunction'
call myfunction(arg1,arg2,...);
print 'myfunction finished'

the program runs perfectly in release mode. If I remove either one of the print statements (or both), I get the above error!

Does anyone have any idea of what might be going on here?

More information:

The function declaration in Fortran is:

!DEC$ ALIAS  myfunction, 'myfunction'

The function declaration in C is:

extern "C" void myfunction(double *arg1, double *arg2, ...)
Foi útil?

Solução

This is a common symptom of a stack corruption. Probably, you are calling myfunction with wrong arguments, but it can be somewhere deeper inside it. Check every call around and check the types of all arguments.

Be sure to use -check all and -warn all with ifort when debugging. That includes -warn interfaces which is very helpful in similar problems, but because you are calling a C function it will probably not find the cause here.

EDIT - for 2 argument subroutine:

In your case make sure both arguments are double precision or c_double or real*8 depending on which type of declaration you use. Don't put any third argument there, as your first piece of code might suggest!

The modern way of interfacing this in Fortran 2003 would be:

interface
  subroutine myfunction(arg1, arg2) bind(C, name="myfunction")
    use iso_c_binding
    real(c_double) :: arg1, arg2
  end subroutine
end interface

which is put instead of the older (and optional) external myfunction in the declaration part of your code.

In any case, carefully check the types and correct position of all the arguments, whatever the final number of them is.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top