尝试读取或写入受保护的内存.这通常表示其他内存已损坏.在C++Dll中
Attempted to read or write protected memory. This is often an indication that other memory is corrupt. in C++ Dll
我的代码有问题。当我编译程序时,它会写"AccessViolationException未处理"试图读取或写入受保护的内存。这通常表明其他内存已损坏。">
C#代码:
[DllImport(@"DLLmedian.dll")]
public static extern SCIOX_bitmap median(SCIOX_bitmap image, int size);
private void button2_Click(object sender, EventArgs e)
{
string url = @"img.png";
pictureBox1.Load(url);
SCIOX_bitmap a;
a.height = pictureBox1.Height;
a.width = pictureBox1.Width;
var source = new BitmapImage(new System.Uri(url));
int b = source.Format.BitsPerPixel;
a.color_depth = (4 * b) * b;
a.points = pictureBox1.Handle;
a = median(a, 8);
}
public struct SCIOX_bitmap
{
public int width;
public int height;
public int color_depth;
public IntPtr points;
};
C++代码:
SCIOX_bitmap __stdcall median(SCIOX_bitmap image, int size)
{
SCIOX_bitmap finMap;
finMap.width = image.width;
finMap.height = image.height;
finMap.color_depth = image.color_depth;
finMap.points = new int[finMap.height*finMap.width];
int *valArr = new int[size*size]
for(int i=0; i<image.width; i++)
for(int j=0; j<image.height; j++)
{
for(int k=0,n=0; k<size; k++)
for(int l=0; l<size; l++,n++)
valArr[n] = image.points[((i-size/2+k+image.width)%image.width)*image.height + (j-size/2+l+image.height)%image.height];
**//AccessViolationException was unhandle**
mysort(valArr, size*size);
if(size*size%2 == 1)
finMap.points[i*finMap.height + j] = valArr[size*size/2];
else
finMap.points[i*finMap.height + j] = (valArr[size*size/2-1] + valArr[size*size/2]) / 2;
}
return finMap;
}
struct SCIOX_bitmap{
int width;
int height;
int color_depth;
int* points;
};
有人帮我吗?
对不起,我忘了SCIOX_bitmap是结构体。SCIOX_bitmap也在c++代码中
public struct SCIOX_bitmap
{
public int width;
public int height;
public int color_depth;
public IntPtr points;
};
似乎您正在访问PictureBox类的Handle,就好像它是Image的句柄一样。这是不对的,Handle属性是控件的句柄。
如果你想获得图像本身的句柄,你需要将其转换为位图,然后使用GetHBitmap()
Bitmap bitmap = new Bitmap(pictureBox1.Image);
a.points = bitmap.GetHBitmap();
此外,我建议您研究C++代码的"三条规则",因为您将在每次调用median
时泄漏为点缓冲区声明的内存。
相关文章:
- 内存效率表示最短路径的方法?
- 内存中的长长类型表示形式
- 将两位数字转换为低内存表示的最快方法
- read() 上的不同行为取决于写入不可写内存时表示文件、匿名管道或套接字的文件描述符
- 指针内存表示和继承铸造
- 如何在计算机内存中打印长双重表示的二进制表示
- 尝试读取或写入受保护的内存.这通常表示其他内存已损坏.在C++Dll中
- DB表的内存表示形式的容器
- 如何从文件表示构建内存中的树
- 已尝试读取或写入受保护的内存.这通常表示其他内存已损坏 DllImport
- 内存中一个像素图像数据的表示形式
- 布尔值在内存中是如何表示的
- 标准::表示 2D 数据的矢量中的内存泄漏
- 内存地址的数字表示和对齐之间的关系
- 表示一个地址需要多少内存
- C++.NET包装程序:试图读取或写入受保护的内存.这通常表示其他内存已损坏
- 指向单链表中没有变量表示每个元素的内存地址的指针
- C++并集表示数据内存与C标量变量类型
- 间接操作符是否更改内存表示
- c++对象实例成员函数的内存表示