Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qprocess runs correctly in debug but in release mode it doesn't run correctly



  • Hi guys
    It's my first time posting here, I have been looking up for an answer but couldn't find one answer that could solve my problem.
    I'm tryin to write a code to attach touch devices into serial ports and manage them.
    But when I run the inputattach in my qt in debug mode, it works properly and when I run "xinput list" it shows me the added device but in relaese mode it doesn't work : here is the part of my code:

    Void Widget::on_button_clicked()
    {
    Qstring serialLocation= "/dev/ttyS0";
    QString touchName= "elotouch";
    QProcess setSerial ; 
    QString args = Qstring("inputattach --%1 %2 --daemon --always").arg(touchName) .arg(serialLocation);
    setSerial.start("/bin/bash", QStringList()<< ("-c") << args << "\n");
    setSerial.waitForStarted();
    setSerial.waitForReadyRead();
    setSerial.waitForFinished();
    
    QProcess *inputList= new QProcess(this);
    inputList-> setProgram("/bin/bash");
    inputList-> start();
    inputList->write("xinput list");
    inputList->write("\n");
    inputList->closeWriteChannel();
    
    ui-> text Edit->insertPlainTextEdit( inputList-> readAllStandardOutPut());
    }
    

    I even used readyReadStandardOutput signal once but it seems like the problem is s.th else.
    I would appreciate if someone help me


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    First thing to do: add error checks to your code.
    You are doing the assumption that everything goes well all the time which is wrong.

    Next, split the arguments properly. Each space in your string means that it should be a separated element in the argument list as explained in QProcess's documentation.



  • @SGaist thanks for your reply, how should I add error check?!
    I ran the code in terminal and it is fine,
    Would you please explain a little more? I can't wrap my head around the reason behind my code running fine in debug and not in release


  • Lifetime Qt Champion

    @TahmDev
    Hi
    Hook up to the signal
    https://doc.qt.io/qt-5/qprocess.html#errorOccurred
    and see if it list something when it don't work.


  • Lifetime Qt Champion

    Please take the time to read the class documentation:

    QProcess::errorOccurred
    QProcess::error
    etc.

    As for your code, it will run faster in release than debug mode. The way you wrote your second QProcess code is wrong. You assume that it has ended after the call to closeWriteChannel which is an error.

    You should learn how to use it in asynchronous mode.

    By the way, you are "leaking" QProcess objects there. They will only be destroyed when the parent is which means you can still fill memory with them each time you call on_button_clicked.



  • @mrjj hi, thanks for your reply.
    I did it but it doesn't show any errors.



  • @TahmDev said in Qprocess runs correctly in debug but in release mode it doesn't run correctly:

    QProcess *inputList= new QProcess(this);
    inputList-> start();

    I seem to be the only person commenting on this, but what does this do? You are using overload https://doc.qt.io/qt-5/qprocess.html#start-2, which says

    Starts the program set by setProgram() ...

    and https://doc.qt.io/qt-5/qprocess.html#setProgram says:

    Set the program to use when starting the process. This function must be called before start().

    Show me how you (apparently) reckon inputList->write("xinput list"); runs xinput list? So I don't understand how you are running any program at all for this? Whether this is connected to debug/release I cannot say....

    [EDIT See my topic https://forum.qt.io/topic/118969/qprocess-with-no-program-to-run for this issue which conused me and may confuse any reader of the code!]



  • @SGaist I read the documents, I'm not a pro, but I wrote many connect signal slots for error occured and state changed to my processes ... Everything works fine but the problem is that the second processes starts soon...if you can help me I would be grateful



  • Guys thanks for all your advices ...I figured out the problem, the linux command related to inputattach makes inputattach to run in background and returns to qt so fast that the attachment has not happened yet.
    I added a timer to my program and after 1000ms the other process will be initiated.
    Everything is fine then


  • Lifetime Qt Champion

    @TahmDev said in Qprocess runs correctly in debug but in release mode it doesn't run correctly:

    I added a timer to my program and after 1000ms the other process will be initiated.
    Everything is fine then

    You know that this is going to haunt you ?
    Did you check my suggestion of using QProcess asynchronous nature to manage the commands you are running ?



  • @SGaist as a matter of fact it did... Then I used Qthread::msleep(500);
    I will try using it asynchronously today and get back to you with the result...