Important: Please read the Qt Code of Conduct -

Calling QCoreApplication::arguments causes undefined behavior

  • I have a Qt program with this code:

    int main(int argc, char **argv){
        QApplication app(argc, argv);
        //Some code
        app.arguments();    //I know that this isn't supposed to do anything, it's just for debugging purposes
        return app.exec();

    Sometimes this code runs just fine, sometimes it crases at the app.arguments() line. Why does QCoreApplication::arguments crash like that and what can I do about it?

    I'm using Qt 5.10.1 on 64-bit Windows.

  • Lifetime Qt Champion

    @Donald-Duck It shouldn't crash.

    Are you sure //Some code is not the reason for the crash?

  • @Donald-Duck said in Calling QCoreApplication::arguments causes undefined behavior:


    app.arguments(); doesn't supposed to cause any crash. Maybe your other code before that line cause the problem.

  • @aha_1980 @TobbY I'm sure that arguments() that crashes. That's at least what the debugger says:

    alt text

    As you can see, arguments() is the first function in there that isn't part of my code. Apparently arguments() calls other Qt code which causes the program to crash.

    What's also strange is that when it doesn't crash, app.arguments().size() is 0.

  • @Donald-Duck said in Calling QCoreApplication::arguments causes undefined behavior:


    So, if you comment out app.arguments(); application run without any crash ? right

  • I've found the part that causes the problem, but I don't understand why it causes a problem. It's apparently because app is of a type that inherits from QApplication and not of type QApplication directly and because I connected to a signal somewhere. Here is a minimal example:

    #include <QDebug>
    #include <QApplication>
    class MyApplication: public QApplication{
        MyApplication(int argc, char **argv): QApplication(argc, argv){}
    int main(int argc, char **argv){
        MyApplication app(argc, argv);  //If I change MyApplication to QApplication, it works as expected
        QObject::connect(&app, &QApplication::applicationDisplayNameChanged, [](){});  //It does the same thing with any other signal, but without this line it works as expected
        qDebug() << app.arguments().size();  //This should print 1 or more, but it either prints 0 or crashes
        return 0;

    The expected behavior would be to print 1 (or more than 1 if I run it with arguments), but it either prints 0 or crashes (it seems completely random whether it prints 0 or crashes, but it never prints 1 as expected). If I either make app of type QApplication instead of MyApplication or if I comment out the QObject::connect line, it behaves as expected.

  • Hi,

    simply add & (pass as reference) in your constructor and it will work perfectly. that is the only culprit in your code.

    class MyApplication: public QApplication{
        MyApplication(int &argc, char** argv): QApplication(argc, argv)
            qDebug() <<"Hi "<<argc;

Log in to reply