编译的Matlab函数只能运行一次 [英] Compiled Matlab function works only once

查看:596
本文介绍了编译的Matlab函数只能运行一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Matlab函数编译到C库中.我正在使用C#应用程序中的该库.

I have a Matlab function compiled into C library. I am using this library from C# application.

如果我第一次在C库中调用函数,则一切正常,但第二次调用会导致异常-mlfMyfunc返回指向结果的空指针插入指针(即使在mlfMyfunc调用之后,output1和output2参数均为IntPtr.Zero)

If I call my function in C library for the first time, everything works fine, but the second call causes an exception - mlfMyfunc returns null pointer insted pointer to results (output1 and output2 parameters are IntPtr.Zero even after mlfMyfunc call)

我的DoubleArray类(围绕mx...函数的包装器)经过了充分的测试,我认为它可以正常工作.

My DoubleArray class (wrapper around mx... functions), is well tested and I think it works correctly.

您是否知道问题可能在哪里?

Do you have any idea where problem could be?

谢谢.卢卡斯

C#代码:

using Native;

 class MatlabAlgosBridge {
   [DllImport("Algos.dll"]
   private static extern bool AlgosInitialize();

   [DllImport("Algos.dll")]
   private static extern void AlgosTerminate();

   [DllImport("Algos.dll")]
   private static extern bool mlfMyfunc([In] int nargout, ref IntPtr output1, ref IntPtr output2, [In] IntPtr xVar, [In] IntPtr time, [In] IntPtr algoParam, [In] IntPtr Ts, [In] IntPtr codes);

  public List<double> Analyze(List<double> xValues) {
    double[] result = null;
    try {
      Native.Mcl.mclInitializeApplication("NULL", 0)
      AlgosInitialize();

      DoubleArray xValM = DoubleArray.CreateMatrix(xValues.Data.Count, 1);
      // Other parameter initialization 

      IntPtr output1 = IntPtr.Zero;
      IntPtr output2 = IntPtr.Zero;

      mlfMyfunc(2, ref output1, ref output2, xValM.Pointer, time.Pointer, params.Pointer, ts.Pointer, codes.Pointer);

      result = new MArray(output1).AsDoubleVector();
    }
    finally {
      AlgosTerminate();
      Native.Mcl.mclTerminateApplication();
    }

    return result;
   }
}

解决方案:

问题是由重复的Matlab引擎初始化引起的.每次我调用Analyze函数时,引擎都会初始化(Native.Mcl.mclInitializeApplication],甚至在finally块中被正确终止(Native.Mcl.mclTerminateApplication),重复初始化也会出问题.内置的matlab函数仍然可以正常工作,但是我的库不要.

The problem was caused by repetitionary Matlab engine initialization. Each time I call Analyze function the engine gets initialized (Native.Mcl.mclInitializeApplication] and even it's being properly terminated (Native.Mcl.mclTerminateApplication) in finally block, something goes wrong with repetitionary initialization. Built in matlab functions still works properly, but my library don't.

该解决方案将mclInitializeApplication调用移至Analyze函数之外,并确保在应用程序生存期内仅调用一次.

The solution is moving mclInitializeApplication call outside Analyze function and ensuring it's called only once in application lifetime.

推荐答案

该问题是由重复的Matlab引擎初始化引起的.每次我调用Analyze函数时,引擎都会被初始化(Native.Mcl.mclInitializeApplication),甚至在finally块中被正确终止(Native.Mcl.mclTerminateApplication)时,重复初始化也会出错.内置的matlab函数仍然可以正常运行,但是我的库无法正常工作.

The problem was caused by repetitionary Matlab engine initialization. Each time I call Analyze function the engine gets initialized (Native.Mcl.mclInitializeApplication) and even it's being properly terminated (Native.Mcl.mclTerminateApplication) in finally block, something goes wrong with repetitionary initialization. Built in matlab functions still works properly, but my library don't.

解决方案是将mclInitializeApplication调用移到Analyze函数之外,并确保在应用程序生存期内仅调用一次.

The solution is moving mclInitializeApplication call outside Analyze function and ensuring it's called only once in application lifetime.

这篇关于编译的Matlab函数只能运行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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