QProcess crashes
-
I'm trying to verify if the process is already running, if it's running closes and creates a new process.
@QProcess *MyProcess;@
@ if(MyProcess!= NULL)
{MyProcess->kill(); } MyProcess = new QProcess(this);@
My application crashes!
-
Just because the pointer "MyProcess" is not NULL, doesn't mean it's a valid pointer!
It might be a dangling pointer, i.e. a pointer to an object that has already been deleted/destroyed.
Any attempt to dereference a dangling pointer will result in undefined behavior (crash).
-
Besides the fact that you don't actually initialize MyProcess variable to NULL (maybe in your code you do) it's not the way to check if process is running. there is a method state() that returns current state of the process - not running, running or starting.
-
Even if the checking process status he continues crashing
@ if(MyProcess->state() == QProcess::Running)
{MyProcess->kill(); }@
-
You still are dereferencing the "MyProcess" pointer. But is that pointer valid at all ???
I mean: Do you assign the address of a QProcess object to that pointer before you try to dereference?
Also, as said before, you also have problem if the object has already been deleted/destroyed...
(If you get a crash on pointer access, it's almost always either a NULL pointer or a dangling pointer!)