Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29267 Discussions

DLL returns different values in Debug vs Release mode

tkibedi
Novice
991 Views
I have a very simple fortran console program, which calls a DLL created in MS C++. Here is the Fortran code:
 Implicit None
 CHARACTER (LEN=256) :: SolutionFile
 REAL (KIND=8) :: Xvalue,Yvalue
 REAL (KIND=8) :: Solution,Xcomponent,Ycomponent
 REAL (KIND=8) :: DBYDY,DBYDX,DBXDY
 Integer (KIND=4) :: Iostat1 
! -----------------------------------------------------------------------------
! Interface for the fld_msvc.dll
Interface
 Integer*4 function INTERP(SolutionFile,Xvalue,Yvalue, &
          Solution,Xcomponent,Ycomponent,DBYDY,DBYDX,DBXDY);
   !DEC$ ATTRIBUTES DLLIMPORT, ALIAS : '_INTERP@36' :: INTERP
   CHARACTER(LEN=256),INTENT(IN) :: SolutionFile
   !DEC$ ATTRIBUTES REFERENCE :: SolutionFile
   REAL (KIND=8),INTENT(IN) :: Xvalue,Yvalue
   REAL (KIND=8),INTENT(OUT) :: Solution,Xcomponent,Ycomponent
   REAL (KIND=8),INTENT(OUT) :: DBYDY,DBYDX,DBXDY
  End Function INTERP
End Interface

! -----------------------------------------------------------------------------
 SolutionFile='htest1.t35'
 Do
   Write(6,'(a)') ' X-coordinate to extrapolate:'
   Read (5,*) Xvalue
   Write(6,'(a)') ' Y-coordinate to extrapolate:'
   Read (5,*) Yvalue
   Iostat1 = INTERP(SolutionFile, &
                   Xvalue, Yvalue, &
                   Solution, Xcomponent, Ycomponent, &
		     DBYDY, DBYDX, DBXDY)
  Select Case (IoStat1)
! -----------------------------------------------------------------------------
!  0	The operation completed successfully.
! -----------------------------------------------------------------------------
  Case (0)
     Write(6,1001)Xvalue, Yvalue, Solution, Xcomponent, Ycomponent, DBYDY, DBYDX, DBXDY
1001 FORMAT('   Xvalue  Yvalue  Solution       Xcomponent     Ycomponent     DBYDY          DBYDX          DBXDY'/&
    1x,2f8.4,6 d15.7)
! -----------------------------------------------------------------------------
!  Non zero - error
! -----------------------------------------------------------------------------
  Case Default
    Stop
  End Select
 End Do
Stop
End

The only non-default compiler option is used: /iface:nomixed_str_len_arg.

Starting the above program in MS Visual Studio 6/Debug in some cases gives different result compare to executing the Release version, or executing the Debug version of the program (without the Debugger). The difference occurs, when the two input parameters are set to Xvalue=Yvalue=0.0. In the first case, starting the program in the MS Visual Studio Debugger I`ve got:
Xvalue Yvalue Solution Xcomponent Ycomponent DBYDY DBYDX DBXDY
0.0000 0.0000 -0.2096539D+00 0.1148147D+01 0.1508939D+05 -0.5771677D+00 -0.2011159D+01 -0.2011159D+01

While running the Release or Debug executable it gives:
Xvalue Yvalue Solution Xcomponent Ycomponent DBYDY DBYDX DBXDY
0.0000 0.0000 0.0000000D+00 0.0000000D+00 0.1508910D+05 0.0000000D+00 0.0000000D+00 0.0000000D+00


Any idea, how to resolve this problem?

I am using CVF 6.6b on a Win2000 system

0 Kudos
3 Replies
Steven_L_Intel1
Employee
991 Views
I'd guess that you have some uninitialized variables being used in the C++ code. Can you debug the C++ code?

Steve
0 Kudos
tkibedi
Novice
991 Views
The DLL will be part of the next release of the Poisson Superfish code. It has been developed somwhere else and I do not have access to the source code.

One idea we should try to explore is to insert a test output into the DLL to confirm, that the actual input values of 0.0000000D+00 passed from the Fortran code to the DLL are slightly different or not.

Am I correct, that debugging mixed language programs can be very tricky. I have some experince in VB programs calling CVF DLLs. To debug the CVF DLL the VB executable is required. I am not sure, if in the case of MS C++ DLL + CVF executable how the debugging works. For example, debugging the DLL in MS C++, the executable of the Fortran program would be used? If this is the case, I would expect that the DLL would return the right values.

0 Kudos
Jugoslav_Dujic
Valued Contributor II
991 Views
The principle of debugging is the same and I don't think it's particularly tricky; if you have CVF and VC++ installed, and sources to both .exe and .dll (regardless of what combination of languages), and both projects in the same workspace, you can debug them smoothly, as if they were one. For other combinations of languages, you can debug one at a time (since you have to use two debuggers).

However, since you don't have the C++ source, and since the error is there, I don't see that you can do anything but to complain to the dll vendor. As Steve said, it's probably due to an uninitialized stack or heap variable (probably an array, since compiler can check uninitialized scalars at compile time), like:
int* pi;
...
pi = new int[10];
pi[0] = something;  //the array is initialized only partly
if (pi[1] >= 0) {
  DoSomething;      //pi[1] is a random value; DoSomething
                    //will be executed at random
}

While debugging, the uninitialized heap memory tends to contain large negative numbers; DoSomething will not be executed. When ran from Explorer, there are usually zeros, and DoSomething will be executed. This is just an observation and don't take it as a rule -- but it did happen to me few times and these errors are really tough to debug.

Jugoslav
0 Kudos
Reply