PInvoke Fortran code with arrays pointers

Announcements and discussion for PInvoke

Moderators: Luke Jefferson, Robert

PInvoke Fortran code with arrays pointers

Postby Andrei » Fri Apr 13, 2012 5:05 am

Hi,

I have a Fortran code with a structure which contains quite a few fields with array pointers, like:

TYPE STRUCTURE
SEQUENCE
INTEGER, POINTER :: ARR1(:)
REAL(8), POINTER :: ARR2(:)
!...
END TYPE STRUCTURE

SUBROUTINE FO(ARR1,ARR2)
!DEC$ ATTRIBUTES DLLEXPORT :: FO
INTEGER, POINTER :: ARR1(:)
REAL(8), POINTER :: ARR2(:)

INTEGER :: N, M
TYPE (STRUCTURE) :: STRUC

N = ... ! some dynamic value
M = ... ! some dynamic value
IF(.NOT.ASSOCIATED(ARR1)) ALLOCATE ARR1(N)
STRUC%ARR1 => ARR1
IF(.NOT.ASSOCIATED(ARR2)) ALLOCATE ARR2(M)
STRUC%ARR2 => ARR2
!...
! Some actions including reallocation of pointer arrays
!...
ARR1 => STRUC%ARR1
ARR2 => STRUC%ARR2
END SUBROUTINE FO

and I have to do multiple calls to this routine from C#, storing the information of that fortran structure.
I've started first with using IntPtr pointers in my C# code having them as many as pointer array fields in STRUCTURE.
Say if 2 fields are considered as above I have the following signature for C# code:

...

[DllImport("Fortran.dll", EntryPoint = "FO", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
public static extern void fo(IntPtr arr0, IntPtr arr1);
...

private IntPtr[] pntFortran = new IntPtr[2];

for(int i=0; i<2; i++) {
pntFortran[i] = Marshal.AllocHGlobal(0);
}

fo(pntFortran[0], pntFortran[1])

// some action, but pointers "pntFortran" are not touched and stay in memory.

fo(pntFortran[0], pntFortran[1])

// few such calls to "fo"

.....................................................................

I don't have any evident problems and looks like IntPtr pointers are updated properly when pointer arrays in
fortran code are allocated/reallocated/deallocated. But, at some point (depends!!!) I have a heap corruption error.

Any hint will be much appreciated.

Thanks!
Andrei
 
Posts: 2
Joined: Fri Apr 13, 2012 3:54 am

Postby Brian Donahue » Mon Apr 16, 2012 10:32 am

Hi,

I can't explain this heap corruption problem - I'd suggest asking on one of the Microsoft forums. This forum primarily used to request new modules or report problems with the PInvoke Wiki.
Brian Donahue
 
Posts: 6670
Joined: Mon Aug 23, 2004 10:48 am

Postby Andrei » Mon Apr 16, 2012 9:28 pm

Brian,

Thanks for looking into this.
I'll try to ask Microsoft forums.

Andrei
Andrei
 
Posts: 2
Joined: Fri Apr 13, 2012 3:54 am


Return to PInvoke.Net

Who is online

Users browsing this forum: No registered users and 1 guest