LOOK
LOOK

Reputation: 17

Attempted to read or write protected memory. This is often an indication that other memory is corrupt. in C++ Dll

i have a problem in code. When i compile program so it write "AccessViolationException was unhandle" "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

C# code:

    [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++ code:

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;
    };

Anybody help me please ?


Sorry I forgot SCIOX_bitmap is struct. SCIOX_bitmap is also in c++ code

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

Upvotes: 0

Views: 782

Answers (1)

The Forest And The Trees
The Forest And The Trees

Reputation: 1856

It appears to be that you're accessing the Handle of a PictureBox class as if it were a handle to the Image. That isn't correct, the Handle property is a handle to the Control.

If you want to get a Handle to the Image itself, you'll need to convert it to a Bitmap and then use GetHBitmap()

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

Additionally, I would recommend that you look into the Rule of Three for your C++ code, as you're going to leak the memory declared for the points buffer on each call to median.

Upvotes: 0

Related Questions