How to debug a native OpenGL crash in managed code?

1

I am currently writing a game rendering engine using LWJGL 3 and Kotlin. Everything works fine for multiple minutes, until out of nowhere the program exits with the following message:

Process finished with exit code -1073740940 (0xC0000374)

All I do is load a few models, and then render them with glDrawElements(...) in the main loop - nothing else is loaded or changed.

Now, I know that this error code means heap corruption, but I do not even get a hs_err_pid logfile and the Java Debugger just crashes with the program. So how would I go about debugging such a crash? Could this be because of an incompatibility with Kotlin?

kotlin
lwjgl
asked on Stack Overflow Mar 16, 2021 by Twometer

1 Answer

1

So, for everyone who may find themselves in a similar situation: Thanks to the LWJGLX debug tool by Kai Burjack, I instantly found what crashed the program.

What I did was the following: In the shader class, when uploading a matrix, I allocated a managed FloatBuffer, which I then accidentally tried to free manually:

val buf = BufferUtils.createFloatBuffer(16)
matrix.get(buf)
glUniformMatrix4fv(location, false, buf)
MemoryUtil.memFree(buf)

The MemoryUtil.memFree() call actually doesn't crash, but as the matrix changes every frame, this method corrupts the heap over time - hence the crash after a few minutes.

I attached the LWJGLX debugger and now my program crashed instantly - with a precise error message telling me that I am trying to free a Memory Region i did not allocate with memAlloc() - So after changing my code to

val buf = MemoryUtil.memAllocFloat(16)
...
MemoryUtil.memFree(buf)

everything now works. I can only recommend the LWGLX debugger - It also found some memory leaks I now have to fix ;-)

answered on Stack Overflow Mar 17, 2021 by Twometer

User contributions licensed under CC BY-SA 3.0