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.

Cosmo
  • 836
  • 1
  • 12
  • 27
  • Try creating a class where you inherit from QOpenGLWidget, which you then can use in your MainWindow. Qt5 has a good docu how to do this: https://doc.qt.io/qt-5/qopenglwidget.html#details I do not know, if this is causing your problem, but I never saw an implementation like yours – RoQuOTriX Aug 27 '19 at 13:22
  • @RoQuOTriX Thanks. In fact I found this problem in another complex project. Which ran successfully on another Windows 10 computer but crushed on my computer. In that project, a subclass of `QOpenGLWidget` is implemented. But it crushes the program. So I created this simple demo to show the problem. I think inherit `QOpenGLWidget` in this demo will not solve the problem, but I'll try and report if it makes different. – Cosmo Aug 29 '19 at 09:54
  • if it crashes only on particular pc's you should check your drivers and OpenGL implementations. Oftens such problems can be avoided with updating drivers – RoQuOTriX Aug 30 '19 at 09:53
  • 1
    @RoQuOTriX That's right! Updating drivers solved my problem. It is strange that we all didn't realize that it is the driver's problem. – Cosmo Sep 01 '19 at 06:56

2 Answers2

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.

Cosmo
  • 836
  • 1
  • 12
  • 27
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.

Aleph0
  • 5,816
  • 4
  • 29
  • 80
  • Thanks for your advice. 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](https://drive.google.com/file/d/1VxYH7MOTmp71gtGU3ja16qF_L6k_M6Fn/view?usp=sharing). – Cosmo Aug 29 '19 at 10:07
  • 1
    Thanks again. I did try this workaround, but it was no use. The codes are commented out in my `main.cpp`. Besides, my platform is Windows instead of some other platforms like Mac OS. So I think that note is not for my case. Sad.... – Cosmo Aug 30 '19 at 03:22
  • @cosmozhang: I just had another idea! I just extended my post .Maybe you can try this and also you might show your loaded modules. – Aleph0 Aug 30 '19 at 10:25
  • That's a brilliant discovery! But it also failed. What's interesting is that, updating my graphic driver in Device Manager (let Windows search and update the driver automatically) solved my problem. – Cosmo Sep 01 '19 at 06:54