| Author |
Message |
Andrei
Joined: 13 Apr 2012 Posts: 2
|
Posted: Fri Apr 13, 2012 5:05 am Post subject: PInvoke Fortran code with arrays pointers |
|
|
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 |
|
 |
Brian Donahue
Joined: 23 Aug 2004 Posts: 6344 Location: Red Gate Software
|
Posted: Mon Apr 16, 2012 10:32 am Post subject: |
|
|
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
Technical Support
Red Gate Software Ltd.
44 (0)870 160 0037 ext 8521
US and CAN 1-866-RED GATE ext 8521 |
|
| Back to top |
|
 |
Andrei
Joined: 13 Apr 2012 Posts: 2
|
Posted: Mon Apr 16, 2012 9:28 pm Post subject: |
|
|
Brian,
Thanks for looking into this.
I'll try to ask Microsoft forums.
Andrei |
|
| Back to top |
|
 |
|
|
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