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. [SOLVED] Segmentation Fault with QProcess, but in console works all fine

[SOLVED] Segmentation Fault with QProcess, but in console works all fine

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 7.5k Views
  • 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.
  • I Offline
    I Offline
    I-sty
    wrote on last edited by
    #1

    Hi all,

    I would like to create a simple app what writes the windows processes.

    @#include <QtDebug>

    #include <QTimer>
    #include <QProcess>
    #include <QPlainTextEdit>
    #include <QString>
    #include <QStringList>

    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    //
    // Global variables
    //
    QString p_stdout;

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    QProcess proc;
    runScript();

    //QTimer *timer = new QTimer(this);
    //connect(timer, SIGNAL(timeout()), this, SLOT(refreshList()));
    //timer->start(10000);
    
    ui->setupUi(this);
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::splitText(QString text)
    {
    QStringList list;

    list = text.split(QRegExp("\\s+"));
    
    for (int i = 0; i < list.size(); i += 3)
    {
        QString temp = list[i];
        temp.append(" ");
        temp.append(list[i+1]);
        temp.append(" ");
        temp.append(list[i+2]);
        temp.append("\n");
        ui->textBox->setPlainText(temp);
    }
    

    }

    void MainWindow::runScript()
    {
    //proc->setReadChannelMode(QProcess::SeparateChannels);
    proc.start("WMIC path win32_process get Caption,Processid,Commandline"); //this command list the processes
    //connect (proc,SIGNAL(finished(int)), this, SLOT(stopped()));
    connect(&proc,SIGNAL(readyReadStandardOutput()),this,SLOT(readyReadStandardOutput()));
    connect(&proc,SIGNAL(readyReadStandardError()),this,SLOT(readyReadStandardError()));

    proc.waitForFinished(-1); //here are the "Segmantation Fault"
    
    if (proc.exitStatus() == QProcess::NormalExit)
    {
        QApplication::beep();
    }
    else
        MainWindow::close();
    
    QByteArray p_stdout = proc.readAll();
    QString p_stderr = proc.readAllStandardError();
    splitText(p_stdout);
    
    /*if (!proc->waitForFinished())
    {
        ui->textBox->setPlainText(QString(proc->readAllStandardError()));
        qDebug() << QString(proc->readAllStandardOutput());
        qDebug() << proc->exitCode();
        qDebug() << proc->exitStatus();
    }
    else
    {
        qDebug() << proc->readAll();
        QByteArray result = proc->readAll();
    }*/
    

    }

    void MainWindow::readyReadStandardOutput(){
    qDebug() << proc.readAllStandardOutput();
    ui->textBox->setPlainText(proc.readAllStandardOutput());
    }

    void MainWindow::readyReadStandardError(){
    qDebug() << proc.readAllStandardError();
    ui->textBox->setPlainText(proc.readAllStandardError());
    }

    void MainWindow::refreshList()
    {
    runScript();
    ui->textBox->update();
    //ui->textBox->setPlainText(p_stdout);
    }

    void MainWindow::on_actionE_xit_triggered()
    {
    MainWindow::close();
    }
    @

    The code is actualy not complete, because I can not debug by the error.

    Thanks,
    I-sty

    Sorry, for my bad English. My first language is Hungarian.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      AlekseyOk
      wrote on last edited by
      #2

      Hi.
      You declared local variable @proc@ in constructor and try to use it in another function! This code shouldn't compile.

      P.S. also take a look to "global QProcess...":http://www.qtcentre.org/threads/11696-QProcess-*proc-at-top-of-source-(global-)

      1 Reply Last reply
      0
      • I Offline
        I Offline
        I-sty
        wrote on last edited by
        #3

        @void MainWindow::runScript()
        {
        QProcess *proc = new QProcess();

        proc->start("WMIC path win32_process get Caption,Processid,Commandline");
        proc->waitForReadyRead();
        
        QString p_stdout = proc->readAll();
        QString p_stderr = proc->readAllStandardError();
        splitText(p_stdout);
        

        }@

        Thanks Aleksey.
        I declared local in constructor, I changed the wairForFinished() to waitForReadyRead() and now it's working :D


        But, ....

        @void MainWindow::splitText(QString text)
        {
        QStringList list;

        list = text.split(QRegExp("\\s+"));
        
        for (int i = 3; i < list.size(); i += 3)
        {
            QString temp = list[i];
            temp.append(" ");
            temp.append(list[i+1]);
            temp.append(" ");
            temp.append(list[i+2]);
            temp.append("\n");
            ui->textBox->setPlainText(temp); //here I give "Segmentation Fault"
        }
        

        }@

        I get a SegFault. Why?

        Sorry, for my bad English. My first language is Hungarian.

        1 Reply Last reply
        0
        • JeroentjehomeJ Offline
          JeroentjehomeJ Offline
          Jeroentjehome
          wrote on last edited by
          #4

          Hi, normally you get the segmentation fault if you want to write or read data from a non-allocated memory location. Why don't you set a break point before the ui->... line and check if the ui pointer is there at all.
          Btw there is no check of a full list, so what happens if the list gives e.g. 20 results in stead of 21? The last index of the list will not be there causing the temp.append(list[i+2]) to read from unallocated or at least wrong data.
          greetz and enjoy live!

          Greetz, Jeroen

          1 Reply Last reply
          0
          • I Offline
            I Offline
            I-sty
            wrote on last edited by
            #5

            I changed the QPlainText to QTextEdit.

            @ for (int i = 3; i < list.size(); ++i)
            {
            /QString temp;
            temp.append(list[i]);
            temp.append(" ");
            temp.append(list[i+1]);
            temp.append(" ");
            temp.append(list[i+2]);
            temp.append("\n\r");
            /
            ui->textBox->setText("hello"); //SegFault
            }@

            But, I give segfault, anyway. :(

            Sorry, for my bad English. My first language is Hungarian.

            1 Reply Last reply
            0
            • F Offline
              F Offline
              franku
              wrote on last edited by
              #6

              In which context do you call void MainWindow::refreshList() ? If you write to the ui not within the gui thread, you also can get a segment fault. This makes sense, since your console application does not fail.

              This, Jen, is the internet.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                AlekseyOk
                wrote on last edited by
                #7

                Also, you have a memory leak in runScript() method.
                Do you really need to allocate proc in heap?

                1 Reply Last reply
                0
                • I Offline
                  I Offline
                  I-sty
                  wrote on last edited by
                  #8

                  @void MainWindow::runScript()
                  {
                  proc->start("WMIC path win32_process get Caption,Processid,Commandline");
                  proc->waitForReadyRead();
                  QApplication::beep();
                  //splitText(QString(proc->readAllStandardOutput()));
                  }
                  @

                  I changed proc.

                  @
                  MainWindow::MainWindow(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::MainWindow)
                  {
                  ui->setupUi(this);

                  proc = new QProcess();
                  
                  ....
                  

                  }@

                  Sorry, for my bad English. My first language is Hungarian.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    AlekseyOk
                    wrote on last edited by
                    #9

                    Memory leak still exists))

                    try to change to:

                    @
                    proc = new QProcess(this);
                    @

                    Did you run your app in debug mode (e.g. via F5 shortcut)?

                    1 Reply Last reply
                    0

                    • Login

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