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. QProcess segmentation fault in QThread
Forum Updated to NodeBB v4.3 + New Features

QProcess segmentation fault in QThread

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 5 Posters 1.6k 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.
  • E E106JZ

    I'm battling a bug where I sometimes get a segmentation fault on QProcess readyReadStandardOutput()/readyReadStandardError() signals when I run it inside a thread and I'm not sure what I'm doing wrong. Below is a minimal example of the crash in a contrived example:

    class Process : public QObject
    {
        Q_OBJECT
    public:
        Process() = default;
        ~Process() = default;
        void start();
    };
    
    void Process::start()
    {
        QProcess process;
    
        connect(&process, &QProcess::readyReadStandardOutput, this, [&]()
        {
            QByteArray readStdOut = process.readAllStandardOutput();
        });
        connect(&process, &QProcess::readyReadStandardError, this,[&]()
        {
            QByteArray readStdErr = process.readAllStandardError();
        });
    
        QStringList args;
    
        args << ("+vm");
        args << ("-hFs1");
        args << ("file.txt");
    
        process.setProgram("D:\\test\\program.exe");
        process.setArguments(args);
        process.start();
    
        if (!process.waitForStarted(-1))
        {
            Q_ASSERT(false);
        }
        if (!process.waitForFinished(-1))
        {
            Q_ASSERT(false);
        }
    
    }
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QThread* thread = new QThread;
        Process process;
        process.moveToThread(thread);
        thread->start();
        process.start();
    
        thread->quit();
        thread->wait();
        delete thread;
    
        MainWindow w;
        w.show();
        return a.exec();
    }
    
    

    Can anyone spot what I'm doing wrong?
    Thanks

    Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @E106JZ said in QProcess segmentation fault in QThread:

    process.start();

    This is wrong - it executes the function in the main thread. You have to use signals and slots to call a function in another thread as clearly described in the documentation to QThread. But why do you need a thread here at all?

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    E 1 Reply Last reply
    2
    • Christian EhrlicherC Christian Ehrlicher

      @E106JZ said in QProcess segmentation fault in QThread:

      process.start();

      This is wrong - it executes the function in the main thread. You have to use signals and slots to call a function in another thread as clearly described in the documentation to QThread. But why do you need a thread here at all?

      E Offline
      E Offline
      E106JZ
      wrote on last edited by
      #3

      @Christian-Ehrlicher Thanks, I didn't realise I was doing that. In my case yes I need a background thread to run this lengthy QProcess so I can update the main thread GUI with the progress.

      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @E106JZ said in QProcess segmentation fault in QThread:

        yes I need a background thread to run this lengthy QProcess so I can update the main thread GUI with the progress.

        Still no reason for a QThread - QProcess is async.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        E 1 Reply Last reply
        3
        • Christian EhrlicherC Christian Ehrlicher

          @E106JZ said in QProcess segmentation fault in QThread:

          yes I need a background thread to run this lengthy QProcess so I can update the main thread GUI with the progress.

          Still no reason for a QThread - QProcess is async.

          E Offline
          E Offline
          E106JZ
          wrote on last edited by
          #5

          @Christian-Ehrlicher How so? I create a progress window (a QDialog that calls exec()) and inside it has a worker that calls the QProcess. Without a QThread, the QProcess runs and blocks the dialog from appearing until its finished. Can it be done without a QThread?

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #6

            A QProcess does not block anything - it's async.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            E 1 Reply Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher

              A QProcess does not block anything - it's async.

              E Offline
              E Offline
              E106JZ
              wrote on last edited by
              #7

              @Christian-Ehrlicher But if we call waitForFinished() on it then it will block the current thread I mean.

              J.HilkJ 1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @E106JZ said in QProcess segmentation fault in QThread:

                But if we call waitForFinished() on it then it will block the current thread I mean.

                Then don't call it - it's not needed.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                1
                • E E106JZ

                  @Christian-Ehrlicher But if we call waitForFinished() on it then it will block the current thread I mean.

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #9

                  @E106JZ said in QProcess segmentation fault in QThread:

                  But if we call waitForFinished() on it then it will block the current thread I mean.

                  that's the point, waitforfinished is the synchronous version of QProcess, you want to listen to the finished signal, https://doc.qt.io/qt-5/qprocess.html#finished, which is asynchronous


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  3
                  • K Offline
                    K Offline
                    kuzulis
                    Qt Champions 2020
                    wrote on last edited by
                    #10

                    Besides, your QProcess instance destroyed after the start() called...

                    jsulmJ 1 Reply Last reply
                    0
                    • K kuzulis

                      Besides, your QProcess instance destroyed after the start() called...

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      @kuzulis No, he waits for finished:

                      if (!process.waitForFinished(-1))
                      

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      2

                      • Login

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