reibisch's picture

VBO AccessViolationException

Hey guys,

I was wondering if any of you had encountered one of these before (exception text is "Attempted to read or write protected memory. This is often an indication that other memory is corrupt"). I noticed in a search that this had been discussed previously, but I don't believe it's directly related to what I'm seeing.

Essentially, I'm trying to convert a ton of immediate mode calls over to use VBOs and I receive this exception on either a DrawArrays or DrawElements call. I've packed the buffering/drawing code purposefully close together to see if it helps (it doesn't). They typically reside in separate functions. Code follows:


uint VertexBufferIndex = 0;
uint ElementBufferIndex = 0;
uint[] TriIndices = new uint[this.tris.Length*3];
Vector3[] verts = new Vector3[this.numVerts];
GL.GenBuffers(1, out this.VertexBufferIndex);
GL.GenBuffers(1, out this.ElementBufferIndex);


GL.BindBuffer(BufferTarget.ElementArrayBuffer, this.ElementBufferIndex);
GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(this.TriIndices.Length * sizeof(uint)), this.TriIndices, BufferUsageHint.StaticDraw);
GL.BindBuffer(BufferTarget.ArrayBuffer, this.VertexBufferIndex);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(this.verts.Length * 8 * sizeof(float)), this.verts, BufferUsageHint.StaticDraw);
//GL.DrawElements(BeginMode.Triangles, (int)this.numTris, DrawElementsType.UnsignedInt, 0);
GL.DrawArrays(BeginMode.Triangles, 0, (int)this.numTris);

The error only crops up on the draw calls. If I comment both out, drawing and sim both continue on happily without error (albeit not displaying this object) and I can subsequently even read/write to/from the arrays that trigger the exception. GL.GetError returns no error.

Any ideas would be much appreciated. I can provide additional detail if required.

EDIT: fixed typo :)


Comment viewing options

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

You need to setup your attribute offsets prior to DrawArrays/DrawElements (check approach #2 in the previous link).

reibisch's picture

Many thanks. :) Works wonderfully now.

On to shaders :/