Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Deleting object after QQmlEngine quit. How?



  • 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?


  • Moderators

    Either move this line:

    ABackend* oBackend = &ABackend::mInstance();
    

    Before this:

    QQmlApplicationEngine oEngine;
    

    Or (better) parent oBackend under oEngine. You'll need to expose it's constructor, then do it like so:

    auto oBackend = new ABackend(&oEngine);
    


  • @sierdzio Just it? I though there would be something very complicated related to Signal-Slots ... I am definitely in need of getting rest ... Started forgetting how objets parenting in Qt. Thx. Issue closed.


  • Moderators

    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.


Log in to reply