QOpenGLWidget crushes on shown

1

I embedded a QOpenGLWidget in a QMainWindow instance w. Compilation is OK. But when debugging, the program crushes at w.show().

The error dialog says:

Exception thrown at 0x000007FEE5AF4469 (d3d11sdklayers.dll) in TestQtOpenGL.exe: 0xC0000005: Access violation reading location 0x00000806E5B03A98.

The call stack is:

d3d11sdklayers.dll!000007fee5af4469()   Unknown
d3d11sdklayers.dll!000007fee5ad2971()   Unknown
libGLESv2d.dll!000007fed7d58a8d()   Unknown
libGLESv2d.dll!000007fed7d56313()   Unknown
libGLESv2d.dll!000007fed7d55521()   Unknown
libGLESv2d.dll!000007fed7b2308a()   Unknown
libGLESv2d.dll!000007fed7b24f1b()   Unknown
libGLESv2d.dll!000007fed79dbba8()   Unknown
libGLESv2d.dll!000007fed7bf2678()   Unknown
libEGLd.dll!000007feecd1209d()  Unknown
qwindowsd.dll!000007fed86752ac()    Unknown
qwindowsd.dll!000007fed8674906()    Unknown
qwindowsd.dll!000007fed8602d55()    Unknown
qwindowsd.dll!000007fed8602999()    Unknown
qwindowsd.dll!000007fed860223b()    Unknown
qwindowsd.dll!000007fed86020fe()    Unknown
Qt5Guid.dll!000007fed93a18a0()  Unknown
Qt5Widgetsd.dll!0000000057d55dc2()  Unknown
Qt5Widgetsd.dll!0000000057d97037()  Unknown
Qt5Widgetsd.dll!0000000057d95d45()  Unknown
Qt5Widgetsd.dll!0000000057d439f6()  Unknown
Qt5Widgetsd.dll!0000000057d95fa9()  Unknown
Qt5Widgetsd.dll!0000000057ce700e()  Unknown
Qt5Widgetsd.dll!0000000057ce46c6()  Unknown
Qt5Cored.dll!0000000058c15af6() Unknown
Qt5Cored.dll!0000000058c20c32() Unknown
Qt5Widgetsd.dll!0000000057d520b0()  Unknown
Qt5Widgetsd.dll!0000000057d523f0()  Unknown
Qt5Widgetsd.dll!0000000057d3dd71()  Unknown
Qt5Widgetsd.dll!0000000057d3e0c9()  Unknown
Qt5Widgetsd.dll!0000000057d50b12()  Unknown
Qt5Widgetsd.dll!0000000057d52411()  Unknown
Qt5Widgetsd.dll!0000000057d3dd71()  Unknown
Qt5Widgetsd.dll!0000000057d3e0c9()  Unknown
> TestQtOpenGL.exe!main(int argc, char * * argv) Line 9 C++ TestQtOpenGL.exe!WinMain(HINSTANCE__ * __formal, HINSTANCE__ * __formal, char * __formal, int __formal) Line 104    C++
[External Code]

A demo:

// main.cpp

#include "TestQtOpenGL.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{

    /*
     * According to some solutions, I have tried the following
     * codes, but no use.
     */
    // QSurfaceFormat format; format.setDepthBufferSize(24);
    // format.setStencilBufferSize(8);
    // format.setVersion(4, 3);
    // format.setProfile(QSurfaceFormat::CoreProfile);
    // QSurfaceFormat::setDefaultFormat(format);

    QApplication a(argc, argv);
    TestQtOpenGL w;
    w.show();
    return a.exec();
}

// TestQtOpenGL.h
#pragma once
#include <QtWidgets/QMainWindow>
#include <qopenglwidget.h>

class TestQtOpenGL : public QMainWindow
{
    Q_OBJECT
public:
    TestQtOpenGL(QWidget *parent = Q_NULLPTR);
private:
    QOpenGLWidget *glwidget;
};
// TestQtOpenGL.cpp
#include "TestQtOpenGL.h"

TestQtOpenGL::TestQtOpenGL(QWidget *parent)
    : QMainWindow(parent)
{
    setFixedSize(800, 600);
    glwidget = new QOpenGLWidget(this);
    setCentralWidget(glwidget);
}

Environment:

  • Qt 5.9.4 msvc2015 64bit
  • Visual Studio 2015
  • Windows 7

According to advice from @Aleph0 , I have investigated the loaded modules by using [Visual Studio]->[Debug]->[Windows]->[Modules]. But I didn't find any suspicious DLLs. The list of the modules is uploaded here.

c++
qt
qtopengl
asked on Stack Overflow Aug 27, 2019 by cosmozhang • edited Aug 30, 2019 by cosmozhang

2 Answers

2

Inspired by @RoQuOTriX, I solved this problem in a very simple way:

Update the graphic driver in Windows Device Manager. Then restart. Recompile and the problem is gone.

Note that my graphic card is Intel HD Graphics. If you are using discrete graphics, you may need to get your graphic driver updates from your device provider.

answered on Stack Overflow Sep 1, 2019 by cosmozhang
1

After reading the documenation of QOpenGLWidget I found the following passage, that might be important to you:

Note: Calling QSurfaceFormat::setDefaultFormat() before constructing the QApplication instance is mandatory on some platforms (for example, macOS) when an OpenGL core profile context is requested. This is to ensure that resource sharing between contexts stays functional as all internal contexts are created using the correct version and profile.

Maybe you should rewrite your main.cpp and putting setDefaultFormat before the creation of the QApplicationobject.

#include "TestQtOpenGL.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
    // This should be come first on some platforms
    QSurfaceFormat format; format.setDepthBufferSize(24);
    format.setStencilBufferSize(8);
    format.setVersion(4, 3);
    format.setProfile(QSurfaceFormat::CoreProfile);
    QSurfaceFormat::setDefaultFormat(format);

    QApplication a(argc, argv);

    TestQtOpenGL w;
    w.show();
    return a.exec();
}

After considering your call stack I figured out, that there is a libGLESv2d.dll, which is not listed in my loaded modules. I tried switching to OpenGL ES by setting:

#include "TestQtOpenGL.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
    QSurfaceFormat format; format.setDepthBufferSize(24);
    format.setStencilBufferSize(8);
    format.setVersion(4, 3);
    format.setProfile(QSurfaceFormat::CoreProfile);
    format.setRenderableType(QSurfaceFormat::RenderableType::OpenGLES);
    QSurfaceFormat::setDefaultFormat(format);
    // Setting surface format before creation of QApplication

    QApplication a(argc, argv);  

    TestQtOpenGL w;
    w.show();
    return a.exec();
}

This resulted in the error messages:

QOpenGLWidget: Failed to create context
QOpenGLWidget: Failed to create context
qt.qpa.backingstore: composeAndFlush: QOpenGLContext creation failed
qt.qpa.backingstore: composeAndFlush: makeCurrent() failed
qt.qpa.backingstore: composeAndFlush: makeCurrent() failed

I suggst, that you change your RenderableType to paint OpenGL and see what happens.

answered on Stack Overflow Aug 28, 2019 by Aleph0 • edited Aug 30, 2019 by Aleph0

User contributions licensed under CC BY-SA 3.0