Red Gate forums :: View topic - PInvoke Fortran code with arrays pointers
Return to www.red-gate.com RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation
PInvoke.Net
PInvoke.Net forum

PInvoke Fortran code with arrays pointers

Search in PInvoke.Net forum
Post new topic   Reply to topic
Jump to:  
Author Message
Andrei



Joined: 13 Apr 2012
Posts: 2

PostPosted: Fri Apr 13, 2012 5:05 am    Post subject: PInvoke Fortran code with arrays pointers Reply with quote

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!
Back to top
View user's profile Send private message
Brian Donahue



Joined: 23 Aug 2004
Posts: 6641

PostPosted: Mon Apr 16, 2012 10:32 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Andrei



Joined: 13 Apr 2012
Posts: 2

PostPosted: Mon Apr 16, 2012 9:28 pm    Post subject: Reply with quote

Brian,

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

Andrei
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group