[SOLVED] QProcess exits a few seconds after starting



  • Hello,

    I uses QProcess to start a new process as with the code below. I know my spawned process works fine when I run it independent of the Qt GUI. For some reason when I try to start the process from my Qt GUI, it starts and runs for about a second then just exits. The error code I get from waitForStart is "2: The last waitFor...() function timed out. The state of QProcess is unchanged, and you can try calling waitFor...() again". Any thoughts? Also, the funny thing is if I use start() instead of startDetached() the program doesnt even start. Maybe someone knows why this may be as well?

    @ QObject *parent;
    QString program = ""C:\Documents and Settings\Ryan\My Documents..."";
    QString workingDir = ""C:\Documents and Settings\Ryan\My Documents..."";
    QProcess *controllerProcess = new QProcess(parent = 0);
    controllerProcess->setWorkingDirectory(workingDir);
    controllerProcess->startDetached(program);
    if(!controllerProcess->waitForStarted(10000))
    {
    qDebug() << "Controller process failed to start!";
    qDebug() << "Process State: " << controllerProcess->state() << " " << "Error Code: " << controllerProcess->error() << " " << "Exit Status: " << controllerProcess->exitStatus();
    return -1;
    }@

    Thanks in advance!



  • Hmm,
    Because we are unable to really test the code it's a bit guessing what's the problem.
    The startDetached should work like you see. The problem might be that your controllerProcess is unallocated when your functions end. Qt is smart enough to also kill your other program with it.
    The waitForStarted() might actually signal a proper start of your other program, but get's destroyed because of the earlier mentioned issue. Make sure your QProcess variable is a class member and active when your external program needs to run.
    Greetz



  • I made it a member of the class now. Here is my code:

    program.c->
    @BasicGui::BasicGui(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::BasicGui)
    {
    ui->setupUi(this);

    createControllerProcess();
    

    }

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

    void BasicGui::createSharedMemory()
    {
    int size = sizeof(double);

    if(sharedMemory.isAttached()) {sharedMemory.detach();}
    
    sharedMemory.setKey("SharedMemory");
    
    if(!sharedMemory.create(size, QSharedMemory::ReadWrite))
    {
        qDebug() << "Unable to create shared memory segment!";
        qDebug() << "Error code: " << sharedMemory.errorString();
    }
    

    }

    void BasicGui::createControllerProcess()
    {
    QObject *parent;
    QString program = ""C:\Documents and Settings..."";
    QString workingDir = ""C:\Documents and Settings..."";

    controllerProcess = new QProcess(parent = 0);
    controllerProcess->setWorkingDirectory(workingDir);
    controllerProcess->startDetached(program);
    
    Sleep(5000);
    

    }
    @

    program.h->
    @namespace Ui {
    class BasicGui;
    }

    class BasicGui : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit BasicGui(QWidget *parent = 0);
    ~BasicGui();
    QSharedMemory sharedMemory;
    QProcess *controllerProcess;

    private:
    Ui::BasicGui *ui;
    void createSharedMemory();
    void createControllerProcess();
    };

    #endif // BASICGUI_H
    @

    I am thinking that it could be something with losing scope or memory. However, I put a sleep after starting the process and it still ended early before we were even out of the function so I'm not so sure it is scope.


  • Lifetime Qt Champion

    Hi,

    startDetached is a static function, your usage of it is wrong



  • [quote author="SGaist" date="1369829265"]Hi,

    startDetached is a static function, your usage of it is wrong[/quote]

    I am a bit confused on how I should use it. Any suggestions?


  • Moderators

    [quote author="rizoritis" date="1369830356"]
    [quote author="SGaist" date="1369829265"]Hi,

    startDetached is a static function, your usage of it is wrong[/quote]

    I am a bit confused on how I should use it. Any suggestions?[/quote]

    Checkout the documentation of "startDetached":http://qt-project.org/doc/qt-5.0/qtcore/qprocess.html#startDetached
    AFAIK you should use the parameter list for the working dir.



  • Static functions are called:
    @QProcess::startDetached(..)@
    instead of
    @m_process.startDetached(..)@
    Greetz


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.