C#调C++DLL尝试读取或写入受保护的内存。这通常指示其他内存已损坏。

asdfg48_ 2014-10-09 11:09:44
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一维数组大小一样吗,二维呢????是不是我的数据类型对应不正确???

...全文
150 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
asdfg48_ 2014-10-09
  • 打赏
  • 举报
回复
C# 调用的最后一行 ReadImage.predictY(OPtion_Parameter, OPtion_Alpha, B0, X2, Y2);//DLL调用
ouyh12345 2014-10-09
  • 打赏
  • 举报
回复
调试一下,定位崩溃发生的位置
asdfg48_ 2014-10-09
  • 打赏
  • 举报
回复
大神们指导下,自己先顶

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧