Reputation: 300
The exact crash when debugging is:
The inferior stopped because it triggered an exception. Stopped in thread 0 by: Exception at 0x7fed96c6cda, code: 0x0000005: read access violation at: 0x0, flags=0x0.
The exception then points to this line in the below code:
if(QOpenGLContext::currentContext()->isValid())
The below code is enough to reproduce the exception for me. Subclassing the QOpenGLWidget class and making the subclass call initializeGL() once before attempting to access context does not fix problem.
#include <QApplication>
#include <QOpenGLWidget>
#include <QOpenGLContext>
#include <QDebug>
void initialize(QOpenGLWidget * renderArea)
{
renderArea->makeCurrent();
if(QOpenGLContext::currentContext()->isValid())
{
qInfo() << "Valid.";
}
}
int main(int argc, char *argv[])
{
QSurfaceFormat format;
format.setVersion(3,3);
format.setProfile(QSurfaceFormat::CoreProfile);
QSurfaceFormat::setDefaultFormat(format);
QApplication a(argc, argv);
QOpenGLWidget * glw = new QOpenGLWidget;
initialize(glw);
return a.exec();
}
Upvotes: 2
Views: 468
Reputation: 194
QOpenGLWidget
initializes GL resources first time the widget is shown.
So you enough to add glw->show()
before your initialize()
call.
Upvotes: 0
Reputation: 300
I now realize the answer: You have to do all such opengl initialization AFTER the event loop starts.
A fixed code where the "MainWindow" class does all opengl initialization on receiving signal "onEventLoopStarted":
#include "mainwindow.h"
#include <QApplication>
#include <QOpenGLWidget>
#include <QOpenGLContext>
#include <QDebug>
#include <QTimer>
int main(int argc, char *argv[])
{
QSurfaceFormat format;
format.setVersion(3,3);
format.setProfile(QSurfaceFormat::CoreProfile);
QSurfaceFormat::setDefaultFormat(format);
QApplication a(argc, argv);
MainWindow w;
w.resize(512, 512);
w.show();
QTimer::singleShot(0, &w, SLOT(onEventLoopStarted()));
return a.exec();
//Window receives event and begins to initialize.
}
Upvotes: 3