'Program Exception - Acces Violation' Error, but the program runs if it's built with a different fortran compiler

'Program Exception - Acces Violation' Error, but the program runs if it's built with a different fortran compiler

Dear all,

I wrote a programin Fortran to dynamically link a dll written in C++. I am using VS2008 with Intel Visual Fortran Compiler 10.1.019. It compiles / links with no errors. However I get a run-timeerror 'forrtl: severe (157) Program Exception - Access violation' .

The same code compiles/links/and runs with no errors in Compaq Visual Fortran Professional Edition 6.1.0.

Intel VFC debbuger points out that the error is in line 80:

Write (*,100) "ModelSetModelCpIgStandard returns: ", resModelSetComponentName

If instead of that, I write:

Write (*,*)resModelSetComponentName

... the program runs with no errors. Wierd, huh? I have no clue what's going on.

Any help is appreciated,


Program Fortran_Calls_C

Use kernel32 ! Declares Win32 API routines

Implicit None

! Declare the interface to functions in BaseCalc.dll


Function ModelClear ()

Integer ModelClear

End function ModelClear

Function ModelSetModelEOS (Model)

Integer ModelSetModelEOS

Character, Intent(IN) :: Model*7

End function ModelSetModelEOS

Function ModelSetModelCpIgStandard (CpIgStnd)

Integer ModelSetModelCpIgStandard

Character, Intent(IN) :: CpIgStnd*10

End function ModelSetModelCpIgStandard

Function ModelSetComponentName (CompIndex,CompName)

Integer ModelSetComponentName

Character, Intent(IN) :: CompName*8

Integer, Intent(IN) :: CompIndex


End function ModelSetComponentName

End Interface

! De
clare variables

Integer(HANDLE) :: dll_handle

Integer(BOOL) :: free_status

Integer resModelClear, resModelSetModelEOS, resModelSetModelCpIgStandard, resModelSetComponentName

Integer i

Integer ComponentIndex

Character ModelName*7, CpIg*10, ComponentName*8

! Declare pointers to the functions.

Pointer (p_ModelClear, ModelClear)

Pointer (p_ModelSetModelEOS, ModelSetModelEOS)

Pointer (p_ModelSetModelCpIgStandard, ModelSetModelCpIgStandard)

Pointer (p_ModelSetComponentName,ModelSetComponentName)

Parameter(ModelName = 'PC-SAFT') ! The selected Model is PC-SAFT

Parameter(CpIg = 'Polynomial') ! The selected Cp model is Polynomial

Parameter(ComponentName = 'n-Hexane') ! Modeling n-Hexane as only component in the system

Parameter(ComponentIndex = 0) !

dll_handle = LoadLibrary ("BaseCalc.dll"C)

! Access function(s) in Library 'BaseCalc.dll'

p_ModelClear = GetProcAddress (dll_handle, "ModelClear"C)

p_ModelSetModelEOS = GetProcAddress (dll_handle, "ModelSetModelEOS"C)

p_ModelSetModelCpIgStandard = GetProcAddress (dll_handle, "ModelSetModelCpIgStandard"C)

p_ModelSetComponentName = GetProcAddress (dll_handle, "ModelSetComponentName"C)

100 Format(A,T60,i5)

200 Format(A,i1,A,T60,i5)

!Call function(s) and get returned value

resModelClear = ModelClear()

Write (*,100) "ModelClear returns: ", resModelClear

resModelSetModelEOS = ModelSetModelEOS(ModelName)

Write (*,100) "ModelSetModelEOS returns: ", resModelSetModelEOS

resModelSetModelCpIgStandard = ModelSetModelCpIgStandard(CpIg)

Write (*,100) "ModelSetModelCpIgStandard returns: ", resModelSetModelCpIgStandard

resModelSetComponentName = ModelSetComponentName(ComponentIndex,ComponentName)

Write (*,100) "ModelSetModelCpIgStandard returns: ", resModelSetComponentName

! Unload the library

free_status = FreeLibrary (dll_handle)

! End of main program


End Program Fortran_Calls_C

17 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

If it works with CVF, then the DLL is using the STDCALL call mechanism. IVF defaults to the C mechanism and if you have a mismatch, you'll corrupt the stack. The symptom you showed is just the sort of thing that a corrupted stack would cause.

Perhaps the easiest thing is to add /iface:cvf to the compile options. Alternatively, add directives to the interfaces to specify STDCALL, REFERENCE, MIXED_STR_LEN_ARG and the appropriate ALIAS.

Steve - Intel Developer Support


/iface:cvf did the trick.



I am trying to convert Fortran code that is written in Compaq visual fortran 6.6.0 to Intel Fortran 10.1. I am able to complile it succesfully but at run time i am getting Access violation Error.

The same program works fine in CVF, i tried setting calling function \iface:cvf but does not help me.

Any suggestions would be appreciated.

1. Consult the write-up at the link Migrating from CVF .

2. Provide source code for a short working example so that the issues can be diagnosed.

3. The following are guidelines for what to expect when changing compilers:

(a) If the source code is all in Fortran and is bug free and self-contained, just using the new compiler should suffice.

(b) If a custom library is used in addition to the Fortran runtime, this library is compatible with CVF, and this library does not use the Fortran runtime, compiling your sources using /iface:cvf should suffice.

(c) If neither (a) nor (b) applies, with luck /iface:cvf may work -- this is worth trying since the effort needed is trivial.

(d) If neither (a) nor (b) aplies, and /iface:cvf does not produce a working application, you have to decide whether

(d1) you should look for IVF-compatible third party libraries, or

(d2) you should spend time writing DEC $ directives to facilitate making correct calls to the libraries, assuming that these calls are documented and your application does not create a conflict between the IVF and CVF runtimes, or

(d3) you should give up.

Hi Thanks for the reply,

After changing the declaration of few variable i am able to debug the program,

I have so many write statements in my program, below are 2 of them,

Write (2,'(" Dummy 1 ,[BTU/kWh]",(",",2x,F10.2))') (OUTPUTS(j)%Res(100+317,1),j=0,NUMOD)

Write (2,'(" Dummy 2 ,[frac]", (",",2x,F10.4))') (OUTPUTS(j)%Res(100+318,1),j=0,NUMOD)

If i comment the second write statement everything works fine, but if i am uncomment second line and try to debug, i am getting access violation at writing location error.

Any suggestions?

You should not expect any suggestions with the scanty information that you gave.

Access violations, in particular, are related to using variables that have not been allocated or are being accessed with improper subscripts, etc. Not knowing how the variables in the two I/O lists were declared and allocated, it is impossible to say why you receive an access violation.

Create and post complete source for a small example that demonstrates the problem, and give information about the OS, platform and compiler versions, compiler options used, and any needed input data files.

Hi astroengineer,

I have just finished a many-month effort to migrate approx 750k sloc from CVF/VS6 to IVF/VS9. There were thousands of minor syntax errors, and roughly 50-100 run time errors (so far), mostly short-circuit, string length and string termination problems. Although there are certainly things I could complain about (e.g. the lack of short-circuit support) I found ZERO defects attributable to the compiler.

For instance, you may have defects lying in CVF code for years. A common one is an unterminated string that sits adjacent to a variable that happens to be a zero, thus terminating the string. A different compiler can choose to locate variables differently, and suddenly you have an access violation in code that has run correctly for 10 years.

When I have run into writelines moving the defect around, it is often been string length problems corrupting the string table. If you are running mixed language FORTRAN/C, I would take a look at any usages of strcpy, and also make sure all your C string lengths are long enough for the string operations you perform. Also, I would run the exe and attach the debugger, and see if the debugger tells you about corruption near . One more thing is to turn on stack trace and see if the crash report helps you. Finally, the brute force method is to copy your codebase somewhere temporary, and comment things out until the defect goes away.

Thanks everyone for your reply, after a week struggle find out the issue, multiple write statement is the culprit,

Write (2,'(" Dummy 1 ,[BTU/kWh]",(",",2x,F10.2))') (OUTPUTS(j)%Res(100+317,1),j=0,NUMOD)

In the above line is written to write the above line multiple times. For example if NUMOD is 2, then write statementwill writethe same line 3 times. If i remove in all my write statement everything working fine.

How to write same line multiple times in Intel Fortran? Any help would be appreciated.

If you want a definite answer, you will have to post complete code. There is nothing wrong with the two lines of code that you showed, other than that a non-standard extension (VFE) has been used, but you have not shown the declarations of the variables in the I/O list, and that is where the probable cause of error is to be looked for.

If you are exceeding the bounds of the arrays in the I/O lists, you are likely to have puzzling consequences such as access violations, program hang ups, etc.

>For example if NUMOD is 2, then write statementwill writethe same line 3 times.

No. Once you get the bugs fixed and run the program, you will not see multiple lines as you stated, because a format group with a definite repeat-count prefix simply repeats the format group the specified number of times, and there are no new line format specs ('/') there.

If you want multiple lines, you have to make them happen by writing a suitable format specification or by changing the WRITE statement.

I don't think it applies to this case, but I will point out that if there are still items to transmit when the format reaches the closing parenthesis, "format reversion" will cause a new line to start and the format will repeat back to the last left parenthesis. It's possible that this is involved in what is being seen in the actual program, but I don't see how it would from the snippet we were shown.

Steve - Intel Developer Support

The poster asked (at the end of #8) "How to write same line multiple times in Intel Fortran?" and stated (earlier in #8) that the reason for using a VFE was to output multiple lines.

I concluded that he wished to write multiple lines, but, because of the access violation did not actually see any corresponding output; so format reversion is not yet known to be an issue.

Thanks mecej4 and Steve for your replies,

I understand with information i shared it is difficult to pin point the error but then my employer policy dont allow me to post my entirecode.

Somehow removing solved write access problem, as you pointed out correctly this not printing same line multiple time but print same format of result.

For example, Write (2,'(" Dummy 2 ,[frac]", (",",2x,F10.4))') (OUTPUTS(j)%Res(100+318,1),j=0,NUMOD), print result as(if NUMOD = 2)

Dummy 2 200 400 600

After removing from that write statement my results prints like this,
Dummy 2

Any suggestion to print multiple results in same format like tab separated is appreciated.


(i) you take the trouble to describe your wishes and problems less ambiguously, and

(ii) you show more diligence in giving consideration to the advice that has already been offered,

you may find better support here.

Thanks mecej4 and steve for your suggestions,

I understand with single line of code i shared it is difficult for you to pin point the error but then my employer policy dont allow me to share the complete code.

Somehow removing solved write access violation error.

As you pointed out, Write (2,'("Dummy2,[BTU/kWh]",(",",2x,F10.2))') (tCCALC_OUTPUTS(j)%Res(100+317,1),j=0,NUMOD)

will not print the same line 3 times but it will print the result in same format 3 times.

For ex: if NUMOD is 2, then my result is
Dummy2 200 400 600

But after removing i am getting result as,

Any suggestions on printed the result in tab separated format is appreciated

> "Somehow removing solved write access violation error. "

But that amounts to killing the patient to stop his coughing.

You have bugs (in code that you have not shown) that are causing access violations. You have to locate and fix those bugs.

I do not think the write statement you provided is the one you use in your program

Write (2,'("Dummy2,[BTU/kWh]",(",",2x,F10.2))') (tCCALC_OUTPUTS(j)%Res(100+317,1),j=0,NUMOD)

is (ignore newlines)

Write (2,'(
"Dummy2,[BTU/kWh]",*** note single text arg ***
)') (tCCALC_OUTPUTS(j)%Res(100+317,1),j=0,NUMOD)

Your output should contain "Dummy2,[BTU/kWh]"
but it only has "Dummy2"

Please explain?

Jim Dempsey


Leave a Comment

Please sign in to add a comment. Not a member? Join today