Random JVM Crashes with glDrawArrays

I am working on a project and randomly glDrawArrays with cause the JVM to crash. It is not reproducable as far as I know and I don’t know how to fix it. Here is the JVM dump: http://pastebin.java-gaming.org/ced0e509c251c, and here is the offending code:


// in Graphics.java
public void draw(VertexArrayObject object) {
		GL11.glEnable(GL11.GL_TEXTURE_2D);
		GL11.glEnable(GL11.GL_BLEND);
		GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
		GL20.glUseProgram(program);
		GL20.glUniformMatrix4fv(projectionLocation, false, toFloatBuffer(orthographic.val, 0, 1 << 4));
		object.bind();
		GL11.glAlphaFunc(GL11.GL_GREATER, 0.1f);
		GL11.glEnable(GL11.GL_ALPHA_TEST);
		GL13.glActiveTexture(GL13.GL_TEXTURE0);
		GL11.glBindTexture(GL11.GL_TEXTURE_2D, object.getTexture());
@@		object.draw();
		GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
		object.unbind();
		GL20.glUseProgram(0);
	}

// in VertexArrayObject.java
public void draw() {
		if (bufferObjects != null) {
			for (VertexBufferObject object : bufferObjects) {
				if (object == null) {
					continue;
				}
				boolean hasCrop = object.getCropX() >= 0 && object.getCropY() >= 0 && object.getCropWidth() >= 0 && object.getCropHeight() >= 0;
				if (hasCrop) {
					GL11.glEnable(GL11.GL_SCISSOR_TEST);
					GL11.glScissor(object.getCropX(), object.getCropY(), object.getCropWidth(), object.getCropHeight());
				}
				bind();
				object.bind();
				GL11.glBindTexture(GL11.GL_TEXTURE_2D, object.getTexture());
				GL20.glVertexAttribPointer(0, 2, GL11.GL_FLOAT, false, 8 << 2, 0);
				GL20.glVertexAttribPointer(1, 4, GL11.GL_FLOAT, false, 8 << 2, 2 << 2);
				GL20.glVertexAttribPointer(2, 2, GL11.GL_FLOAT, false, 8 << 2, 6 << 2);
@@				GL11.glDrawArrays(object.getShape(), 0, object.getVertices());
				object.unbind();
				unbind();
				if (hasCrop) {
					GL11.glDisable(GL11.GL_SCISSOR_TEST);
				}
			}
		} else {
			if (vertices > 0) {
@@				GL11.glDrawArrays(shape, 0, vertices);
			}
		}
	}

Am I doing something wrong? It renders perfectly fine and works fine 99% of the time, it’s just the occasional JVM crash that confuses me.

You’re probably causing an access violation by accidentally telling the driver to read outside the bounds of a VBO. Check your vertex attributes and glDrawArrays() parameters.

All the times that the draw calls (glDrawArrays, glDrawElements, …) caused a segmentation fault for me was due to a vertex attribute being enabled but not bound to a buffer object.
I cannot see a call to glEnableVertexAttribArray in your code. You must have some arrays enabled but not bound to a buffer object.
Also, usually, it did not crash for me when requesting to draw more vertices than what the bound buffers provided.

No vertex attribute pointer = 100% segfault.
Going out of VBO bounds = 0-100% segfault depending on drivers. Behaves differently on different vendors and GPU generations, hence this fits better.

I wouldn’t say it “fits better,” since you cannot know how and under which circumstances the OP enables or disables the vertex attributes deterministically, since we don’t see that in the code. Both possibilities are equally probable.

If it renders fine 99% of the the time, the attribute pointers have to be set. Most likely the VBO’s size or the vertex count of glDrawArrays() is wrong.

theagentd and KaiHH, thanks for your thoughts, I think that you may be right about the vertex count causing the crash. When I get a chance I will try and trace down the cases in my code that cause it.