dimanche 12 juin 2016

QApplication child objects not destroyed when the system is shut down


I have a Qt application with a single dialog window. The main looks like this:

QApplication a(argc, argv);
MyObject * myMobject = new QObject(&a);
MainWindow w;
w.show();
return a.exec();

MyObject constructor creates a temporary file, and the destructor deletes it.

When I close the application normally by closing the dialog, MyObject destructor is called as expected, as it is a child of QApplication, which is allocated on the stack in main().

However, when the user session is closed (log off, shutdown, ...), the file is not removed, showing that MyObject destructor is never called.

What is the reason for this? I though that Windows was closing every window before session shutdown, but it looks like my process is terminated.

From the documentation, there is the QCoreApplication::aboutToQuit() signal. But this documentation tells that Qt detects the shutdown and exits the event loop, emitting that signal at the very end.

If that's really the case, a.exec() should return, exactly like when the window is closed, and the QApplication should be destroyed with its children, calling MyObject destructor. In other words, as myObject is a child of a, there should not be any need to handle the aboutToQuit() signal.


Aucun commentaire:

Enregistrer un commentaire