Unhandled Exception using glGenBuffer on Release m

2019-07-05 00:50发布

问题:

I'm having some trouble while compiling my project on Windows 7 using Qt 4.8 on Release mode. Everything works fine on Debug, but on Release I get an Unhandled Exception: 0xC0000005: Access violation.

I narrowed it down to the line where this happens, which is when I generate my Pixel Buffers. My first guess would be wrong DLLs loading, but I checked the executable with Dependency Walker and every DLL loaded is correct.

Here goes some of my code:

class CameraView : public QGLWidget, protected QGLFunctions;
void CameraView::initializeGL()
{

    initializeGLFunctions(this->context());

    glGenBuffers(1, &pbo_); //<<<<<  This is where I get the unhandled exception on Release mode
        glBindBuffer(QGLBuffer::PixelUnpackBuffer, pbo_);
        glBufferData(QGLBuffer::PixelUnpackBuffer, 3 * sizeof(BYTE) * image_width_ * image_height_, NULL, GL_STREAM_DRAW);
        ...
}

Again, this works great on debug. Why would this only happen on Release?

回答1:

I got it. Seems like this issue is related to this one: https://forum.qt.io/topic/12492/qt-4-8-qglfunctions-functions-crash-in-release-build

and there's a bug report that may be related also: https://bugreports.qt.io/browse/QTBUG-5729

Perhaps the initializeGLFunctions() method is not getting all function pointers for the GL extension functions, I don't really know why but this seems to be it.

The solution for me was to stop using Qt's GL extensions and start using glew.

So, here's what worked for me:

#include <QtGui/QtGui>
#include <gl/glew.h>
#include <QtOpenGL/QGLWidget>

class CameraView : public QGLWidget;
void CameraView::initializeGL()
{

   //initializeGLFunctions(this->context());
   GLenum init = glewInit();


   // Create buffers
   glGenBuffers(1, &pbo_);

   glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo_);
   glBufferData(GL_PIXEL_UNPACK_BUFFER, 3 * sizeof(BYTE) * image_width_ * image_height_, NULL, GL_STREAM_DRAW);

   // Set matrixes
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();

   glOrtho(0, this->width(), 0, this->height(), 0, 1);
   glClearColor(0.0, 0.0, 0.0, 0.0);
   glShadeModel(GL_FLAT);
   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
}

Be sure that glew.h is included before any QTOpenGL headers or else you'll get a compilation error.



标签: c++ qt opengl qt4