c# - Calling Method from Native dll works first time, instacrashes on second time (due to fortran modules)? -


i'm creating c# net4.5 console project consumes fortran dll. method dll works fine on first call, , instacrashes on second call, no error messages of sort.

running tests, found if don't use module1, meaning put variable declaration on subroutine, works fine, on number of calls

c#

static void main(string[] args)     {         testmyarray();//works flawlesly         testmyarray();//instakills program     }       private static void testmyarray()     {         console.writeline("start");         int size = 52;         float[] myarray = new float[size];         sub_(ref size, myarray);         console.writeline(myarray.select(x => x.tostring()).aggregate((x, y) => x + ";" + y));         console.readline();     }       [dllimport("fortranarraysimpletest.dll", callingconvention = callingconvention.cdecl)]     static extern void sub_(ref int size, float[] myarray); 

fortran

 !dec$ attributes dllexport::ingammaextern subroutine sub(size, myarray)     use module1   ! * removing module usage fixes problem   implicit none integer  :: size integer :: assignme real, dimension(1:size) :: myarray  assignme = size allocate(alocarray(1:assignme))  end subroutine ! ************************************begin file***********       module module1       implicit none   real, dimension(:), allocatable :: alocarray       end module module1 

this solution, removal of modules, extremely cumbersome , maintenance major headache, due code made me post question being large.

environment: gnu fortran compiler, windows 7 64bits, codeblocks fortran, vs2012, didn't change compiler options.

any ideas?

thank time

you should deallocate array allocarray in subroutine before leaving it. module variable used host association in routine, remain allocated after subroutine left. trying allocate again when enter routine second time may cause crash. alternatively, check allocation status via allocated() intrinsic function.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -