C#调C++DLL尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
C#代码:
[DllImport("SVM_DLL.dll", EntryPoint = "ImportantMain", CallingConvention = CallingConvention.Cdecl)]
public static extern void ImportantMain(double[] OPtion_Parameter, double[] OPtion_Alpha, double[] B0, double[] Option_MaxError);
[DllImport("SVM_DLL.dll", EntryPoint = "predictY", CallingConvention = CallingConvention.Cdecl)]
public static extern void predictY(double[] OPtion_Parameter, double[] OPtion_Alpha, double[] B0, double[,] X2, double[] Y2);
FileStream fs = new FileStream("C:\\Users\\Cwq\\Desktop\\处理后的遥感程序\\ForestAppImageClassify\\meanBandValueArr.txt", FileMode.Open, FileAccess.Write, FileShare.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
double[] Option_MaxError = new double[1];
//double[,] KK = new double[30, 300];
double[] Y2 = new double[1];
double[] OPtion_Parameter = new double[6];
double[] OPtion_Alpha = new double[30];
double[] B0 = new double[1];
double[,] X2 = new double[300,300];
int iso = 0;
ReadImage.ImportantMain(OPtion_Parameter, OPtion_Alpha, B0, Option_MaxError);
for (int i = 0; i < 6; i++)
sw.Write(OPtion_Parameter[i] + " ");
sw.WriteLine();
for (int i = 0; i < 30; i++)
sw.Write(OPtion_Alpha[i] + " ");
sw.WriteLine();
sw.WriteLine(" GF3 " + " GF4 " + " GF4/GF3 " + " (GF4-GF3)/(GF4+GF3) " + " 预测值Y2 " + " B0");
X2[0, 0] = (double)pixelValueArr[2] / 255.0;
X2[0, 1] = (double)pixelValueArr[3] / 255.0;
X2[0, 2] = (double)(pixelValueArr[3] / pixelValueArr[2]) / 2.16;
X2[0, 3] = (double)((pixelValueArr[3] - pixelValueArr[2]) / (pixelValueArr[3] + pixelValueArr[2])) / 0.36;
System.Diagnostics.Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); // 开始监视代码
ReadImage.predictY(OPtion_Parameter, OPtion_Alpha, B0, X2, Y2);//DLL调用
程序在执行大约十分钟时最后一行 ReadImage.predictY(OPtion_Parameter, OPtion_Alpha, B0, X2, Y2);//DLL调用
就会出现 “尝试读取或写入受保护的内存。这通常指示其他内存已损坏。” C#源程序需要多次调用C++DLL读取影像图。刚开始可以执行的,但是就在运行一段时间后就会出现这个问题。
C++中的DLL
extern"C"_declspec(dllexport)void predictY(double OPtion_Parameter[6],double OPtion_Alpha[30],double B0[1],static double X2[300][300],static double Y2[1] )
想问下C++中的double型一维数组和C#中的double一维数组大小一样吗,二维呢????是不是我的数据类型对应不正确???