PDA

View Full Version : Deleting QProcess



user_mail07
25th January 2008, 22:13
I have problem when I am deleting QProcesses and I dont get any output. If I comment out deleteLater() method to delete processes I get right output in the form of string as expected. I have tried to use delete and deleteLater to delete processes. But something is still going wrong. I have created two seperate process to read data from external scripts. I am using QT3.


//Declared member Functions globally.
QString strFirstProcess;
QString strSecondProcess;
QProcess process1;
QProcess process2;

QString CMainTemplate::CreateControls()

{
lineEdit->setText(GetFirstProcess());
lineEdit2->setText(GetSecondProcess());
}

//...FIRST PROCESS
QString CMainTemplate::GetFirstProcess()

{

process= new QProcess(this);

//Add the arguments
process1->addArgument( "/bash" );
process->addArgument("GetFirstScript");


if ( !process->start() )
{
qDebug("ERROR: First Process Never Started");
process->deleteLater();
}

connect(process, SIGNAL(readyReadStdout()), this, SLOT(readFirstProcess()));

return strFirstProcess;

}
//...SECOND PROCESS
QString CMainTemplate::GetSecondProcess()

{

process1= new QProcess(this);

//Add the arguments
process1->addArgument( "/bash" );
process1->addArgument("GetSecondScript");


if ( !process1->start() )
{
qDebug("ERROR: Second Process Never Started");
process1->deleteLater();
}

connect(process1, SIGNAL(readyReadStdout()), this, SLOT(readSecondProcess()));

return strSecondProcess;

}
//=====================SLOTS TO READ STD OUT ================================================== =

void CMainTemplate::readFirstProcess()
{
QString str = process->readStdout() ;
strFirstProcess = str;
process->deleteLater();
}
void CMainTemplate::readSecondProcess()

{
QString str = process1->readStdout() ;
strSecondProcess = str;
process1->deleteLater();
}

wysota
25th January 2008, 23:48
You probably kill the process before all output is generated. The signal you connect to is emited when there is any data to read (like 1 character) and not when all data is pending. I suggest you delete the process object only when it tells you the process it controls has ended.

user_mail07
26th January 2008, 00:12
how do i know that particular process has ended its loop or should I delete only once after returning the string I mean data.

wysota
26th January 2008, 00:23
Look at the signals QProcess offers.

user_mail07
28th January 2008, 20:59
Well my application still crash and I get segmentation fault always. I tried processExited and launchedfinished signals too. It is just the matter of deleting qprocessed when I am running sequentially.

I tried few things such as

connect( process, SIGNAL(processExited()),this, SLOT(deleteLater()) );
OR
connect( process, SIGNAL(processExited()),this, SLOT(stopProces()) );




QString CMainTemplate::GetFirstProcess()
{

process= new QProcess(this);

//Add the arguments

process->addArgument( "/bash" );

process->addArgument("GetFirstScript");
if ( !process->start() )
{

qDebug("ERROR: First Process Never Started");

process->deleteLater();
}


connect(process, SIGNAL(readyReadStdout()), this, SLOT(readFirstProcess()));

return strFirstProcess;



}


QString CMainTemplate::stopProces()
{
const QObject *sen=sender();
delete sen;
}

wysota
28th January 2008, 21:54
Have you tried debugging to see where and why it crashes?

user_mail07
29th January 2008, 18:30
I tried to put some debug statements to see where it exactly crash. The segmentaion fault does not provide me any information. As soon as I call CreateContorls( ) to call two sequential process my application crash. It never prints any thing else . It looks like Slots readFirstProcess and readSecondProcess never get called otherwise it should print debug statements from slots such as "Print ReadOut Process1".

The output I receive after calling CreateControls() method is :-

Print Statement 1
Print Statement 2
Print Statement 3
Print Statement 4
Print Statement 5
Print Statement 6

But if executes only one process with GetFirstProcess( ) while commenting out second GetSecondProcess() then there is no problem.

QString CMainTemplate::CreateControls()
{

lineEdit->setText(GetFirstProcess());
//lineEdit2->setText(GetSecondProcess());
}





QString CMainTemplate::CreateControls()
{

lineEdit->setText(GetFirstProcess());
lineEdit2->setText(GetSecondProcess());
}

QString CMainTemplate::GetFirstProcess()
{
process= new QProcess(this);
//Add the arguments
process1->addArgument( "/bash" );
process->addArgument("GetFirstScript");

qDebug("Print Statement 1");
connect(process, SIGNAL(readyReadStdout()), this, SLOT(readFirstProcess()));

qDebug("Print Statement 2");
if ( !process->start() )
{
qDebug("ERROR: First Process Never Started");
process->deleteLater();
}

qDebug("Print Statement 3");

return strFirstProcess;
}

//...SECOND PROCESS

QString CMainTemplate::GetSecondProcess()
{
process1= new QProcess(this);
//Add the arguments
process1->addArgument( "/bash" );
process1->addArgument("GetSecondScript");

qDebug("Print Statement 4");

connect(process1, SIGNAL(readyReadStdout()), this, SLOT(readSecondProcess()));

qDebug("Print Statement 5");

if ( !process1->start() )
{
qDebug("ERROR: Second Process Never Started");
process->deleteLater();
}

qDebug("Print Statement 6");

return strSecondProcess;
}

//=====================SLOTS TO READ STD OUT ================================================== =

void CMainTemplate::readFirstProcess()
{

QString str = process->readStdout() ;
strFirstProcess = str;
qDebug("Print ReadOut Process1);
process->deleteLater();

}

void CMainTemplate::readSecondProcess()
{
QString str = process1->readStdout() ;
strSecondProcess = str;
qDebug("Print ReadOut Process2);
process1->deleteLater();

}

wysota
29th January 2008, 18:55
I tried to put some debug statements to see where it exactly crash.

I meant using a debugger and its stack tracing abilities.