[Mono-list] System.AccessViolationException just under Win32

Andreas Färber andreas.faerber at web.de
Sun Aug 20 09:17:22 EDT 2006


Hi,

> I'm facing this exception:
>
> "System.AccessViolationException: Attempted to read or write  
> protected memory. This is often an indication that other memory is  
> corrupt"
>
> The piece of code involved is the "get" block of the following method:
>
>     public byte[] this[int row, int col]
>     {
>       set
>  {
>    int bpp = (int) pr.bpp;
>    IntPtr dest = (IntPtr) ((int) pr.data + (row - Y) * Rowstride +
>       (col - X) * bpp);
>    Marshal.Copy(value, 0, dest, value.Length);
>  }
>
>       get
>  {
>    int bpp = (int) pr.bpp;
>    IntPtr src = (IntPtr) ((int) pr.data + (row - Y) * Rowstride +
>      (col - X) * bpp);
>    Marshal.Copy(src, _dummy, 0, bpp);
>
>    return _dummy;
>  }
>     }
>
> After several investigations,it seems that the problem is related  
> to "src" variable and the "Marshal.Copy" invocation. The attribute  
> "pr.data" has type "IntPtr" but the block of memory referenced by  
> it is allocated directly by GIMP (in an unmanaged way), so I can't  
> use the method Marshal.AllocHGlobal .

It is always a bad idea to cast between IntPtr and int (Int32),  
rather use long (Int64) with IntPtr. Might not be the problem you are  
facing but might cause problems on 64-bit systems when e.g. your  
pr.data is outside the int range.

Andreas

> It works fine with Linux, but under Win32 it raises that exception.
>
> Thanks in advance for your support,
>
> Regards,
>
>   Max
>
>
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list



More information about the Mono-list mailing list