[Interest] QCoreApplication::arguments(): getting the application name safely during global destruction

Thiago Macieira thiago.macieira at intel.com
Tue Jan 21 08:36:20 CET 2020


On Monday, 20 January 2020 04:48:41 PST René J.V. Bertin wrote:
> Hi,
> 
> I call
> 
>    QString arg0 = qApp? qApp->arguments()[0] : QString();
> 
> in order to get the application name in a function that can also be called
> during the global destruction phase. I've never seen issues with that with
> Qt <= 5.9 but just had a crash with Qt 5.12.6 :

Sorry, it's a bug in the application code.

QCoreApplication keep s a reference to argc. Since main() has exited, the 
reference is now dangling and will likely result in garbage being read. For 
this reason and many others,) QCoreApplication should have been destroyed 
before main() returned.

> Regardless of whether this is a regression in QCoreApplication, is there a
> safe and cross-platform way to get at the app name in code that might be
> called as above?

If you're going to need it on shut down, you should save it when you still 
have access to it. I think glibc has an internal variable that still points to 
argv, but that's not a portable solution. And if your code is a plugin, you 
must also deal with the case where the application modifies argv or passes 
something completely different to QCoreApplication.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel System Software Products





More information about the Interest mailing list