Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glGenBuffers crashes in Release Build

Tags:

c++

opengl

qt4

I am facing an odd problem with the OpenGL function glGenBuffers(). I'm writing a fairly simple application in which I use a VBO declared in the following way:

#include <QGLFunctions>
#include <QGLWidget>

class MyClass : public QGLWidget, protected QGLFunctions {
    GLuint vertexBufferObject;

    // ...
    GLuint makeBufferList(void);
}

GLuint MyClass::makeBufferList(void) {
    vertexBufferObject = 0;
    glGenBuffers(1, &vertexBufferObject);  // <-- Here it crashes

    // ... load data and render

    return vertexBufferList;
}

MyClass::MyClass(QWidget* parent) 
    : QGLWidget(parent),
      vertexBufferObject(0)
{
    QGLContext* context = new QGLContext(this->format());
    initializeGLFunctions(context);
    glInit();
}

MyClass::~MyClass() {
    glDeleteBuffers(1, &vertexBufferObject);
}

This all works perfectly fine in the Debug Build. The data is rendered nicely and all and the programme finishes correctly in the end. However, in Release Build, the glGenBuffers() crashes the programme. It doesn't just return 0 or do nothing, it crashes right at the function call. But since the problem only occurs in Release mode, I can't use the debugger to find out what's going wrong.

I'm working on a Windows 7 system and developing in Qt 4.8.1. The compiler is the MSVC 2010 (Qt SDK) compiler.

Does anyone have any suggestions that I might try?

// Edit:

Maybe useful to know: I tried to compile exactly the same code on a Mac, using the GCC (Qt SDK) compiler, and both the Debug and Release Build work perfectly fine. But on Windows 7, the problem persists.

like image 477
Yellow Avatar asked Oct 07 '22 00:10

Yellow


1 Answers

Found the the trouble (thanks to @MahmoudFayez): the problem comes from a bug in the Qt API, which makes the glGenBuffers() function (and possibly others as well) crash. The question is directly equivalent to the issue discussed here:

  • Unhandled Exception using glGenBuffer on Release mode only - QT
  • http://qt-project.org/forums/viewthread/12794

The solution is relatively simple although not very elegant: use GLEW instead of QGLFunctions. I fixed my problem in the following way:

#include "glew.h"
#include <QGLWidget>

class MyClass : public QGLWidget {
    // ...same as the above
}

MyClass::MyClass(QWidget* parent) 
    : QGLWidget(parent),
      vertexBufferObject(0)
{ 
    makeCurrent();
    glewInit();
}

This solved everything. A disadvantage is that this involves using an extra external dependency, whereas using Qt is all about being as compatible as possible, with as few external dependencies as you possibly can. However, it seems that we need to wait until Qt 5.0 is released before this bug may be fixed.

As a final comment: I have not been able to figure out which part in this bug makes only the Release Build that crashes, but not the Debug mode.

like image 69
Yellow Avatar answered Oct 13 '22 10:10

Yellow