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

Allocatable arrays across dlls

Ranbir_Singh_Shoker
524 Views
I'm trying to pass an allocatable array from my main fortran exe to a fortran dll. The array is supposed to be allocated in the dll, passed back out, and deallocated in the calling routine. However, on entry to the dll routine, the debugger throws a heap error.

The dll code is as follows:

subroutine ExampleLib2 (array)

! Expose subroutine ExampleLib to users of this DLL
!
!DEC$ ATTRIBUTES STDCAll, DLLEXPORT::ExampleLib2
!DEC$ATTRIBUTES REFERENCE :: array

integer, allocatable, dimension(:), intent (out):: array

if (allocated(array)) deallocate(array)

allocate (array(3))

array(2) = 555

return
end subroutine

The main program code is:

program MainApp

implicit none

integer :: j_status
integer, allocatable, dimension(:) :: array2

interface
subroutine examplelib2 (array)

!dec$ attributes stdcall, dllimport::examplelib2
!dec$attributes reference :: array

integer, allocatable, dimension(:), intent (out):: array
end subroutine
end interface

call ExampleLib2 (array2)

if (allocated(array2)) deallocate(array2, stat=j_status)

end program MainApp


Does anybody have any ideas as to why this doesnt work? Can you not
pass allocatable arrays across dlls in this way?

Thanks in advance
Ranbir
0 Kudos
2 Replies
Steven_L_Intel1
Employee
524 Views
You MUST change the main project's property to specify the "Use run-time libraries" Multithread DLL (it must match the DLL's setting). In other words, all Fortran code in the application must be using the same copy of the run-time library.

The default for DLL projects is to link against the DLL libraries, but for EXE projects it's to link against the static libraries.
0 Kudos
Ranbir_Singh_Shoker
524 Views
Yup, that solved my problem...thanks

Ranbir
0 Kudos
Reply