Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Calling QCoreApplication::arguments causes undefined behavior
Forum Updated to NodeBB v4.3 + New Features

Calling QCoreApplication::arguments causes undefined behavior

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Donald Duck
    wrote on last edited by Donald Duck
    #1

    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.

    aha_1980A 1 Reply Last reply
    0
    • D Donald Duck

      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.

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Donald-Duck It shouldn't crash.

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

      Qt has to stay free or it will die.

      D 1 Reply Last reply
      2
      • T Offline
        T Offline
        TobbY
        wrote on last edited by
        #3

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

        app.arguments

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

        1 Reply Last reply
        0
        • aha_1980A aha_1980

          @Donald-Duck It shouldn't crash.

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

          D Offline
          D Offline
          Donald Duck
          wrote on last edited by Donald Duck
          #4

          @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.

          1 Reply Last reply
          0
          • T Offline
            T Offline
            TobbY
            wrote on last edited by TobbY
            #5

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

            app.arguments();

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

            1 Reply Last reply
            0
            • D Offline
              D Offline
              Donald Duck
              wrote on last edited by Donald Duck
              #6

              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{
              public:
                  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.

              1 Reply Last reply
              0
              • T Offline
                T Offline
                TobbY
                wrote on last edited by TobbY
                #7

                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{
                public:
                    MyApplication(int &argc, char** argv): QApplication(argc, argv)
                    {
                        qDebug() <<"Hi "<<argc;
                    }
                };
                
                1 Reply Last reply
                4

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved