[SOLVED] Problem with cmd process
-
void MainWindow::on_submitpath_clicked()
{
QString directory = ui->linepath->text() ;
QString model = ui->linemodel->text();
QString tracing = ui->linetracing->text();
QString taskset = ui->linetaskset->text();
QString sndstring="rtosim_ik_from_file --model ";
sndstring.append(model);
sndstring.append(" --trc ");
sndstring.append(tracing);
sndstring.append(" --task-set ");
sndstring.append(taskset);
sndstring.append(" -v");
QString filenew = directory;
filenew.append("/Filenew.bat");
QFile file(filenew);
if ( file.open(QIODevice::ReadWrite) )
{
QTextStream stream(&file);
stream << sndstring <<endl;
}
QStringList arguments;
arguments << "/c" << "Filenew.bat";
QProcess *exec = new QProcess();
exec->setWorkingDirectory(directory);
exec->start("cmd.exe", arguments);
}SOLVED!! Thanks to all!!
-
@Cimmy said in Problem with cmd process:
SOLVED!! Thanks to all!!
Not really - you leak memory (exec is never deleted). As I wrote already add exec to your class as member, or pointer to QProcess to be able to delete it when you're done.
-
@Cimmy said in Problem with cmd process:
right?
wrong. Then you will again have same issue: QProcess going out of scope and deleted.
I write it now for the third time: "I wrote before: "Simply add QProcess member to your class"." (as @JonB suggested also). -
@Cimmy
If you are interested (as I am!) as to why you have things this way. Here is what @jsulm has been telling you:The issue is the
QProcess
destructor:Destructs the QProcess object, i.e., killing the process.
Note that this function will not return until the process is terminated.
So if a
QProcess
gets destructed it will kill the process if it's still running. The problem is your code is only going tostart()
the sub-process running. It can/will continue running for a while. If your code were waiting for it to finish (e.g.QProcess::execute()
orQProcess::waitForFinished()
), there wouldn't be a problem, after that you could allow theQProcess
to get destroyed.If your
QProcess
is a local variable on the stack in a function like you propose, as soon as the function exits (variable goes "out of scope") the destructor would get called. So you can either:-
Move
QProcess exec
variable to a member of your class, not destructed till class instance destructed; or -
Use
QProcess *exec = new QProcess()
, allocated on the heap. Not destructed tilldelete exec
. But then you need somewhere to save that pointer so that you can later delete it, no use as a local variable, so equally needs moving to class scope.
@jsulm
I have musing over this. If you want simple to start a sub-process and "forget" about it (yes, I know about "zombie" processes), this~QProcess()
behaviour is a bit problematic. I don't thinkstartDetached()
in itself would help here, it doesn't say that the destructor will not kill the process in this case:If the calling process exits, the detached process will continue to run unaffected.
Yes, but if
~QProcess()
called on exit it will still kill it, unless the docs are a bit vague here. Perhaps actually it does not? I wonder ifQProcess()
could do with asetNoKillOrWaitOnDestruct()
flag, ifstartDetached()
does not do that?So.... I guess in this case the only safe thing to do would be to go
new QProcess
and deliberately notdelete
on exit? C++ doesn't go through everything you'venew
ed anddelete
prior to exit, does it?! So accept that your program "leaks" prior to exit (e.g. a memory checker) and put up with it? -
-
@JonB "If the calling process exits, the detached process will continue to run unaffected." - https://doc.qt.io/qt-5/qprocess.html#startDetached
So, the QProcess destructor will not terminate the detached process as it is detached.
"this ~QProcess() behaviour is a bit problematic" - in what way? If you use startDetached() then the destructor doesn't matter. If you use exec() then I don't see why ~QProcess() terminating process is a problem? At the end it's your job as developer to select the right approach. -
"If the calling process exits, the detached process will continue to run unaffected."
That describes what happens if the calling process exits. It does not state it countermands what I quoted from
~QProcess()
, which states it kills & waits. The question (my question) is what happens, which "wins", if you do not usenew
but have a "global" scopedQProcess globProc
variable (not*globProc
), initiateglocProc.startDetached()
, and then exit your program. To me the docs are unclear....Can I try this myself? No, because I'm stinky Python, and there are no variables, only heap pointers....
-
@JonB said in Problem with cmd process:
That describes what happens if the calling process exits
Yes, and if an application exits ~QProcess() will be called (at least if it exits normally)...
It's the whole point of startDetached() - it detaches the QProcess instance from the started process. Just try. -
@jsulm
Ah!! (And you don't think those create an instance internally?). OK, so if I usestatic QProcess::startDetached()
that really should not call~QProcess
, even on program exit?It's the whole point of startDetached() - it detaches the QProcess instance from the started process.
Just because a process is detached that does not mean you cannot wait on or kill it, does it? It just means things like it's in its own session.
But it should not matter as the process is detached and the destructor should NOT terminate it.
OK, but I don't get that from the docs! Maybe we read them differently. I'm also having a deeper think about C++
static
, too long now of having to do Python... :(Time for me to have a play....
-
@JonB said in Problem with cmd process:
And you don't think those create an instance internally?
I don't know. But it should not matter as the process is detached and the destructor should NOT terminate it.
-
@jsulm
Just to confirm your interpretation.From Python/PySide2, from a terminal if I run an interactive
python3
and do>>> from PySide2.QtCore import QProcess >>> p = QProcess(); p.start("./script")
and then exit the python session (python will auto-delete everything created), I get a message
QProcess: Destroyed while process ("./script") is still running.
But if I use
>>> p = QProcess(); p.startDetached("./script") # or >>> QProcess.startDetached("./script")
no message, and I continue to see
./script
's output after the python session has exited. -
@JonB said in Problem with cmd process:
no message, and I continue to see ./script's output after the python session has exited.
This is expected, isn't it? As stated in the documentation. ~QProcess() is called in both cases.
-
This is expected, isn't it? As stated in the documentation. ~QProcess() is called in both cases.
Expected by you apparently, but not by me. If
~QProcess()
is called, docs stateDestructs the QProcess object, i.e., killing the process.
Note that this function will not return until the process is terminated.
If it said "but not when started via (non-
static
)QProcess::startDetached()
" then I would be happy. Like I said, perhaps different doc interpretation between you & me. -
@JonB Well, again:
"If the calling process exits, the detached process will continue to run unaffected." - https://doc.qt.io/qt-5/qprocess.html#startDetachedAnd you even confirmed this behaviour by yourself :-)
You can upload a patch fixing ~QProcess() documentation.
-
@jsulm
I already wrote above: "calling process exits" does not tell you whether~QProcess()
is or is not called. In C++, if Iglob_dangling = new QProcess(); exit(0);
C++ cleanup does not call~QProcess()
, does it?But when
~QProcess()
is called for whatever reason, https://code.woboq.org/qt5/qtbase/src/corelib/io/qprocess.cpp.html#_ZN8QProcessD1Ev hasQProcess::~QProcess() { Q_D(QProcess); if (d->processState != NotRunning) { qWarning().nospace() << "QProcess: Destroyed while process (" << QDir::toNativeSeparators(program()) << ") is still running."; kill(); waitForFinished(); }
so presumably somewhere
qProcess->startDetached()
ends up causingd->processState = NotRunning
. -
@JonB If allocated on the stack destructor ALWAYS is called if app is closing in a clean way. If allocated on the heap you have to delete it. I'm sure Python has clean memory management and deletes what it allocates (so destrcutor is called).
-
@jsulm said in Problem with cmd process:
If allocated on the heap you have to delete it. I'm sure Python has clean memory management and deletes what it allocates (so destrcutor is called).
Indeed exactly. So from C++ you have the choice to
new
somewhere and notdelete
, thereby avoiding destructor being called on exit. In Python you can't help destructor being called. Hence my requirement to understand~QProcess
.As I said, I think we're just differing over what Qt docs might care to say in
~QProcess
entry about what happens whenpProcess->startDetached()
was called. We'd better leave it at that :)