avc81's picture

AccessViolationException and GL.BufferData

GL.BindBuffer(BufferTarget.ArrayBuffer, bufferidarray(4))
GL.BufferData(Of Boolean)(BufferTarget.ArrayBuffer, New IntPtr(rawdata.Edges.Count * Marshal.SizeOf(GetType(Boolean))), edgearray, BufferUsageHint.StaticDraw)

(buffering data for the edge flags array)
Executing theese two statements always leads to an AccessViolationException when edgearray.Count equals to a big number. let's say (58644) .
Is it an opentk known issue or am i doing something wrong?

my pc:
graphic card: nvidia 9500gs
Processor: intel quad 9400


Comments

Comment viewing options

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

i add an odd fact ;):

Marshal.SizeOf(GetType(Boolean)) = 4
the Fiddler's picture
avc81 wrote:

i add an odd fact ;):

Marshal.SizeOf(GetType(Boolean)) = 4

This might be the cause. AFAIK, in .Net, boolean is 1 byte long. Try using rawdata.Edges.Count without multiplying by 4 and see if that works.

Also try OpenTK.BlittableValueType.SizeOf() and see what that returns.

avc81's picture

subsituting Marshal.SizeOf(GetType(Boolean)) with 1 makes it work.
but this is a bug (at least from the vb.net point of view).

i think that there is a conflict between vb.net's boolean and c#'s boolean.
in fact the documented size of boolean in vb.net is 4 bytes

avc81's picture

some investigations and i found out that it is not so simple :
msdn
in c# sizeof will give you correctly 1 but in vb.net there is no sizeof operator

the Fiddler's picture
avc81 wrote:

subsituting Marshal.SizeOf(GetType(Boolean)) with 1 makes it work.
but this is a bug (at least from the vb.net point of view).

i think that there is a conflict between vb.net's boolean and c#'s boolean.
in fact the documented size of boolean in vb.net is 4 bytes

System.Boolean is part of the CLS (common language system), which is respected by both C# and VB.Net. Boolean is defined as a 1 byte structure (but may expanded to 4 bytes or more for alignment reasons).

Marshal.SizeOf returns the unmanaged size for bool, which is 4 bytes (because C doesn't have a bool datatype and you typically emulate it using an int).

GLboolean is defined as at least 1bit, with no other limit on its exact size. All implementations I've seen typedef it to an unsigned char (almost always 1 byte long).

Bottom line: when passing a Boolean to OpenGL, use 1 byte as its size. There's no bug here, just a mismatch in semantics.

avc81's picture

yes you're right it's just important to know that. future opentk vb.net users will find this topic.