簡體   English   中英

嘗試讀取或寫入受保護的內存。 這通常表明其他內存已損壞。 在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是struct。 SCIOX_bitmap也在C ++代碼中

public struct SCIOX_bitmap
    {
        public int width;
        public int height;
        public int color_depth;
        public IntPtr points;
    };

看來您正在訪問PictureBox類的Handle,就像它是Image的句柄一樣。 這是不正確的,Handle屬性是Control的句柄。

如果要獲取圖像本身的句柄,則需要將其轉換為位圖,然后使用GetHBitmap()

Bitmap bitmap = new Bitmap(pictureBox1.Image);
a.points = bitmap.GetHBitmap();

另外,我建議您為C ++代碼研究“三規則” ,因為您將泄漏每次調用median為點緩沖區聲明的內存。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM