The Q*Application instance should be destroyed before main() returns

• Jan 22, 2020 - 09:16
Reported version
3.3
Type
Functional
Frequency
Once
Severity
S3 - Major
Reproducibility
Always
Status
active
Regression
No
Workaround
No
Project

This came up discussing an issue that came up using mscore with a 3rd party Qt plugin not directly related to mscore itself on the Qt interest ML.

Qt assumes that the QCoreApplication instance is destroyed before main() returns; doing this resets qApp to nullptr. Any plugin accessing the instance via qApp during global destruction as part of its cleanup work is likely to access memory that has become invalid and provoke a crash.

While my exact way of triggering this isn't likely to be encountered by many other users it's still a dormant bug that should be addressed.


Comments

Frequency Few Once
Reported version 3.3

> Qt assumes that the QCoreApplication instance is destroyed before main() returns

Could you please provide a reference for this? I have not encountered any statement on this in Qt docs, nor have I seen any issues resulting from this. Moreover, Qt documentation says that exec() function may not return at all so anything that resides after qApp->exec() call may happen to be not executed at all.

The info comes from Thiago Macieira whom I've come to know as one of the references on this kind of thing:

https://lists.qt-project.org/pipermail/interest/2020-January/034409.html

Most code I've seen create the Q*Application instance as a local variable that is destroyed when it goes out of scope. I suppose you could do something like

```
QPointer app = MuseScoreApplication::initApplication(argc, av);
````

in Ms::runApplication()`
(I'm not getting this to format correctly; you should get the intended code when you quote this, sorry).