C 라이브러리에 컴파일 된 Matlab 함수가 있습니다. C# 응용 프로그램에서이 라이브러리를 사용하고 있습니다.컴파일 된 Matlab 함수는 한 번만 작동합니다.
처음으로 C 라이브러리에서 함수를 호출하면 모든 것이 정상적으로 작동하지만 두 번째 호출에서는 예외가 발생합니다. mlfMyfunc는 결과에 대한 포인터를 insted 포인터로 반환합니다 (mlfMyfunc 호출 후에도 output1 및 output2 매개 변수는 IntPtr.Zero입니다.))
내 DoubleArray
클래스 (래퍼 주변 mx...
함수)는 잘 테스트되었으며 제대로 작동한다고 생각합니다.
어디에서 문제가 있는지 알고 계십니까?
감사합니다. 루카스
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;
}
}
솔루션 :이 문제는 repetitionary matlab에 엔진 초기화에 의해 발생 된
. 내가 분석 함수를 호출 할 때마다 엔진이 초기화되고 (Native.Mcl.mclInitializeApplication
) finally
블록에서 제대로 종료 (Native.Mcl.mclTerminateApplication
)되어 있습니다. 반복 초기화로 문제가 발생합니다 .Mylab 함수는 여전히 제대로 작동하지만 라이브러리는 작동하지 않습니다.
이 솔루션은 기능을 분석 mclInitializeApplication
전화 외부 이동하고 애플리케이션 일생에 한 번만라고 보장된다.
제안 해 주셔서 감사합니다. 작동하지 않았습니다. MATLAB 엔진 초기화 문제가있었습니다 - 수정 된 질문보기 –