betafy's picture

Access Violation Exception on GL.DrawElements

I'm currently rewriting what I had for my game's engine, and this time I'm using OpenTK more than I am Tao. I just finished the model portion of my content pipeline, and as I went to test rendering a model, I got an access violation exception on this bit of code: (not sure how useful it is to you, though)

GL.DrawElements(BeginMode.Triangles, polyCount, DrawElementsType.UnsignedShort, IntPtr.Zero);
// Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Does anyone know how I could possibly fix and prevent this? I tried throwing a try/catch block around it so that I could at least print that I was getting the error rather than having the program be interrupted, but I still get the interruption.

Also, if it helps, I'm using the 2012/03/15 nightly build of OpenTK.


Comments

Comment viewing options

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

Make sure you've got your index buffer bound to BufferTarget.ElementArrayBuffer before DrawElements. If you bind a VAO after you bind your index buffer, the VAO will overwrite your bind, so you need to bind it again afterwards.

mOfl's picture

The Access Violation Exception at GL.DrawElements just indicates that there has been an access violation somewhere when drawing. It has nothing to do with this line in particular. Most of the time, one of your Vertex Buffer Objects is not filled or you are indexing elements in your Index Buffer that are out of bounds. Check if you have set up your geometry and its attributes correctly and if the number assigned to polyCount is correct. The name "polyCount" suggests that you pass the number of polygons you want to draw, which would be wrong, because the second parameter of GL.DrawElements is the number of vertex indices to be rendered. Thus, if you have an index buffer, the second parameter should be the index buffer's length.

betafy's picture

EDIT: Huh... I tried modifying everything based on this and it still didn't work, but apparently I didn't modify everything. I just did, and now it works. Thank you both for your help.

betafy's picture

I recently got a new desktop computer and tried running my game on it but immediately got another AccessViolationException on, you guessed it, DrawElements. I also tried DrawArrays. DrawArrays worked on my laptop (which I used previously for development), but neither work on my desktop (I have the pointers setup and everything).

If it helps, the desktop is an AMD FX-4100 with an AMD Radeon HD 6670 graphics card.

Inertia's picture

Please re-read what MOfl said (thanks btw. I'd thumb your posts up)

The problem is something like that you have 2 triangles but only 5 vertices and the access violation occurs at the 6th nonexistant vertex. Please verify your array lengths and strides.

betafy's picture

Previously that error was for model rendering, which works now with no problem on both my laptop and desktop. This new problem is with heightmapping, which worked fine on my laptop. No errors. I've gone over my code, and all of the array creation and storage is correct as far as I can tell. I create a two-dimensional array equivalent to the heightmap image with the information I'd need for displaying it, then I convert it to an array of quads, and from there I convert that into my float array containing all of the information I'd need. I don't use normals or specify an index array, just vertices and texture coordinates. (Could that contribute to my problem? If so, I don't see why as it works on my laptop.)