Boney
Boney

Reputation: 2202

std::bad_alloc exception when creating QQuickWidget

I'm trying to convert a part of an existing Qt QWidget application to QML.

And for this, I'm using QQuickWidget as per the code mentioned here:

QQuickWidget *view = new QQuickWidget();
view->setSource(QUrl::fromLocalFile(":/qml/spinner.qml"));
view->show();

I'm having the above code in the constructor of my main widget.

The code works at times. But some other time, it throws exception in this line:

QQuickWidget *view = new QQuickWidget();

Exception says:

Unhandled exception at 0x00007FFC84314B59 in app.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x000000338BCFDEE0.

I don't have a clue why the exception is happening or why it works at times. I haven't been able to find a pattern so far on when it works and when it doesn't.

What else I have tried so far:

From what I understand, this exception happens when the system is running on low RAM. But I have many gigs of RAM free in my system.

Please provide any inputs if you have.

QT version is: 5.15.2 and Platform is: Windows.

Upvotes: 0

Views: 417

Answers (1)

Boney
Boney

Reputation: 2202

Issue has been fixed, after taking care of the below warning, mentioned here:

Warning: The data referred to by argc and argv must stay valid for the entire lifetime of the QApplication object. In addition, argc must be greater than zero and argv must contain at least one valid character string.

We were not passing argc by reference to the QApplication constructor, which after being taken care of, fixed the issue.

I don't know the exact relation between QQuickWidget and QApplication, but the call stack of a QQuickWidget crash on Mac platform suggested there is relation, after which we found the issue of argc not passed by reference.

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   libsystem_platform.dylib        0x00007fff6752de52 _platform_strlen + 18
1   org.qt-project.QtCore           0x00000001039e66b2 QCoreApplication::arguments() + 130
2   org.qt-project.QtQuick          0x0000000104c65a8e contextFactory() + 46
3   org.qt-project.QtQuick          0x0000000104c661fb QSGContext::createDefaultContext() + 11
4   org.qt-project.QtQuick          0x0000000104d7ba8d QQuickRenderControl::QQuickRenderControl(QObject*) + 93
5   org.qt-project.QtQuickWidgets   0x00000001037d1ed3 QQuickWidgetPrivate::init(QQmlEngine*) + 51
6   com.logi.bolt.app               0x0000000101759066 main + 4134
7   libdyld.dylib                   0x00007fff67337cc9 start + 1

Upvotes: 0

Related Questions