来自本地dll的调用方法第一次工作,第二次instacrashes(由于fortran模块)? [英] Calling Method from Native dll works first time, instacrashes on second time (due to fortran modules)?

查看:307
本文介绍了来自本地dll的调用方法第一次工作,第二次instacrashes(由于fortran模块)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个消耗Fortran dll的C#net4.5控制台项目。这个dll的方法在第一次调用时工作正常,第二次调用时instacrashes,没有任何错误消息。



运行一些测试,我发现

p> 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();


$ b [DllImport(FortranArraySimpleTest.dll,CallingConvention = CallingConvention.Cdecl)]
static extern void sub_(ref int size,float [] myarray );

FORTRAN

 !DEC $ ATTRIBUTES DLLEXPORT :: ingammaextern 
子程序sub(size,myarray)
使用module1! *删除模块用法修复问题
隐式无
INTEGER :: size
integer :: assignme
REAL,dimension(1:size):: myarray

assignme = size
allocate(alocarray(1:assignme))

结束子程序
! ************************************开始另一个文件***********
MODULE module1
IMPLICIT NONE


real,dimension(:),allocatable :: alocarray
END MODULE module1


$ b

这个解决方案,模块的移除,非常麻烦,而且由于代码使得我发布的问题非常令人头痛,大的。
$ b

环境: GNU Fortran编译器,windows 7 64位,CodeBlocks for fortran,VS2012,我没有更改任何编译器选项。 / p>

任何想法?



感谢您的时间

在离开它之前,您应该在子例程中取消分配数组 allocarray 。由于它是您的例程中主机关联使用的模块变量,它将在子例程离开后保持分配状态。尝试在第二次进入例程时再次分配它可能是导致崩溃的原因。或者,您可以通过 allocated()内部函数检查其分配状态。


I'm creating a C# net4.5 console project which consumes a fortran dll. The method from this dll works fine on the first call, and instacrashes on second call, no error messages of any sort.

Running some tests, i found that if i don't use the module1, meaning put the variable declaration on subroutine, it works fine, on any 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 THE PROBLEM
  implicit none
INTEGER  :: size
integer :: assignme
REAL, dimension(1:size) :: myarray

assignme = size
allocate(alocarray(1:assignme))

end subroutine
! ************************************begin another file***********
      MODULE module1
      IMPLICIT NONE


real, dimension(:), allocatable :: alocarray
      END MODULE module1

This solution, the removal of modules, is extremely cumbersome and a maintenance major headache, due to the code which made me post the question being very large.

Environment: GNU Fortran Compiler, windows 7 64bits, CodeBlocks for fortran, VS2012, i didn't change any compiler options.

Any ideas?

Thank you for your time

解决方案

You should deallocate the array allocarray in the subroutine before leaving it. As it is a module variable used by host association in your routine, it will remain allocated after the subroutine is left. Trying to allocate it again when you enter the routine the second time may be the cause for your crash. Alternatively, you could check for its allocation status via the allocated() intrinsic function.

这篇关于来自本地dll的调用方法第一次工作,第二次instacrashes(由于fortran模块)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆