Write names to additional buffer similar to z-buffer

The issue is that the GL_PACK_ALIGNMENT defaults to 4 and you’re allocating 3 bytes per pixel so you’re overrunning the pixel read buffer. If you insert a call to gl.glPixelStorei(GL.GL_PACK_ALIGNMENT, 1); before your call to glReadPixels() that solves the problem.

Well I’ll check this out. The funny thing is, that it’s wokring perfectly without any hectic resizing. Could you point me a bit closer to the problem. I’d like to understand it.

The issue is that for each pixel read back into your buffer, OpenGL is writing 3 bytes and then aligning the destination address to a 4-byte aligned value. Since your buffer is only big enough to hold three bytes per pixel, you’re overrunning the end of your readback buffer and corrupting the C heap. This is causing crashes later when free() is called by the code inside the JDK which deallocates the backing store for your direct buffer.