
AccessViolationException
Posted Monday, 13 July, 2009 - 08:31 by ZX80 inHi all!
I've just found something strange in GL.Bitmap(...) behaviour:
This block of code in about 2-3% of cases causes an "AccessViolationException":
Byte[] arrBytes = new Byte[64];
for (int i = 0; i < arrBytes.Length; i++ )
arrBytes[i] = 0xAA;
GL.Color3(Color.Red);
GL.RasterPos2(10, 10);
GL.Bitmap(16, 32, 0,0,0,0,arrBytes);
This happens rarely but it crashes my app.
When (after this exception is caught by debugger) I'm checking the buffer, debugger shows me that the memory
for "arrBytes" array is properly allocated and filled with data but despite of this an exception (mentioned above) is generated.
Here're 2 top lines of stack trace:
StackTrace:
at OpenTK.Graphics.GL.Imports.Bitmap(Int32 width, Int32 height, Single xorig, Single yorig, Single xmove, Single ymove, Byte* bitmap)
at OpenTK.Graphics.GL.Bitmap(Int32 width, Int32 height, Single xorig, Single yorig, Single xmove, Single ymove, Byte[] bitmap)
Next line is my code in glControl1_Paint(...) shown above.
Any ideas?
OS: Windows XP 32bit
Graphics card: GeForce 6600
Framework: .NET v.3.5 with SP1
IDE: VisualStudio 2008
Thanks,
ZX80.


Comments
Re: AccessViolationException
Are you calling
GL.PixelStoreorGL.PixelTransferanywhere in your code? These methods also affect the decoding of thebyte[]parameter, so make sure to reset them before callingGL.Bitmap.I also can't find any indication whether
GL.Bitmaprelies on client or server storage. If it uses client storage, you are responsible for keeping the address of the 'Byte* bitmap' parameter valid. The .Net garbage collector may collect the 'arrBytes' parameter if it goes out of scope or move it to defragment memory: you will either have to pin it throughGCHandle.Alloc(arrBytes, GCHandleType.Pinned)or allocate the array in unmanaged memory withSystem.InteropServices.Marshal.AllocHGlobal(64). Both methods have significant side-effects, so make sure to read the docs (Marshal.AllocHGlobal is safer but more difficult to use.)Also, strange as this may sound, you are likely to get significantly better performance if you use regular textures instead of bitmaps.
Re: AccessViolationException
Hello, the Fiddler!
Thank you for the answer!
I don't use any PixelStore/PixelTransfer in my code.
And the data of my GL.Bitmap is in client storage, at least i didn't configured any server storage.
So the problem was in my buffer allocation logic really.
I didn't noticed anywhere in docs that the buffer should be available after the GL.Bitmap(...) call is made.
So thank you very much for your help!
Best regards,
ZX80.