ZX80's picture

AccessViolationException

Hi 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

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

Are you calling GL.PixelStore or GL.PixelTransfer anywhere in your code? These methods also affect the decoding of the byte[] parameter, so make sure to reset them before calling GL.Bitmap.

I also can't find any indication whether GL.Bitmap relies 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 through
GCHandle.Alloc(arrBytes, GCHandleType.Pinned) or allocate the array in unmanaged memory with System.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.

ZX80's picture

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.