[Solved] QGLWidget heap overflow
I've been using OpenGL 3.1 inside a QGLWidget. I've just been using normal OpenGL code (eg, glGenBuffers, glDrawElements, glBindVertexAttrib, glEnableVertexAttribArray, etc.) with GLM vecs and mats as opposed to QGL things like QShaderProgram or QMatrix.
I have no problems if I just pass vertex position data to the shader program and just set the color of every fragment to a constant in the fragment shader. But, if I pass vertex color data to the shader program as well as vertex positions, I get a problem. Everything displays correctly, the vertices are the colors they should be, but when I close the main window, my IDE (Visual Studio 2010) stays in debugging mode for several seconds. Then I get a popup saying that Windows has triggered a breakpoint, and that it could be due to a corruption of the heap. In the output window I have something like:
[quote]HEAP[qt_integration_test.exe]: HEAP: Free Heap block 745f840 modified at 745f854 after it was freed[/quote]
I googled the problem and didn't find anything that could help me, but I did come across this:
[quote]Fixed an on-exit crash for apps using GL.
If a QGLWidget is left on the heap when the QApplication destructor is
called, it will leave the QGLWidget in a broken state.
The widget itself is released and set to a non-created state,
which the associated QGLContext doesn't get notified about.
With this patch the QGLWidget knows when QWidget::destroy() is called
and can act acordingly.
Task-number: QT-3498, QTBUG-10995
That seems to describe the problem I'm having. But I can't understand why I get a heap corruption only when I use glEnableVertexAttribArray(1) and/or
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(VertexType), (GLvoid*)(sizeof(VertexType.Position)))
and no error when I just use glEnableVertexAttribArray(0)
And on that site it seems to indicate the issue was solved on 06/11/2010 - I'm using Qt 4.8.3, which is much more recent.
Something wrong is happening when QMainWindow or QGLWidget's destructor is called. The problem is, I can't figure out what. Perhaps it's because I'm not using QGL code but rather GLEW and normal OpenGL functions inside what I guess is a QGLContext. Does anyone have any helpful pointers? Has anyone gotten custom vertex attributes to work inside a QGLWidget without using QGl code?
Problem solved. The issue was that all buffers and shader programs have to be deleted before the OpenGL context is deleted. I managed this by reimplementing QCloseEvent on the main window holding the QGLwidget, then within that event handler calling one of my custom glWidget's functions that cleaned up all the VBOs and VAOs and programs and stuff. :)