Deleting object after QQmlEngine quit. How?
Solved
General and Desktop
-
Hello all!
I have an issue with application quit process.
The object ABackend that is created like this:int main(int inCounter, char* inArguments[]) { #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); #endif QGuiApplication oApplication(inCounter, inArguments); QQmlApplicationEngine oEngine; qInstallMessageHandler(fLoggerMessageHandler); ABackend* oBackend = &ABackend::mInstance(); oBackend->pGuiApplication = &oApplication; oBackend->pEngine = &oEngine; oBackend->pRootContext = oEngine.rootContext(); oBackend->mInit(); const QUrl oURL(QStringLiteral(SCOUT_QML_MAIN)); QObject::connect( &oEngine, &QQmlApplicationEngine::objectCreated, &oApplication, [oURL](QObject *obj, const QUrl &objUrl) { if (!obj && oURL == objUrl) { QCoreApplication::exit(-1); } }, Qt::QueuedConnection ); oEngine.load(oURL); return oApplication.exec();
is deleting faster than QML engine quit properly. And result of it I see this message:
TypeError: Cannot call method 'mSomeMethodOfAbackend' of null
The question is how to delete it only after QML Engine destruction?
-
Either move this line:
ABackend* oBackend = &ABackend::mInstance();
Before this:
QQmlApplicationEngine oEngine;
Or (better) parent
oBackend
underoEngine
. You'll need to expose it's constructor, then do it like so:auto oBackend = new ABackend(&oEngine);
-
Yeah, this started appearing a few versions ago, around 5.13 or 5.14. I think some bug fix must have changed QQmlEngine destructor or something. In every case I noticed these
TypeError
logs, it was enough to change parenting or initialization order to fix the issue.