PDA

View Full Version : Freeing a QProcess after it has finished using deleteLater()



Luc4
12th July 2011, 21:50
Hi! I usually do this to free a QProcess (or a QThread after it has finished):


QProcess* proc = new QProcess();
connect(proc, SIGNAL(finished(int)), proc, SLOT(deleteLater()));

and seems to work just fine, and I suppose is the correct way of freeing memory. Am I right?

Now, I find myself in the situation where I need to do something after the process has finished, but I also want to delete the pointer. Can I do this?


QProcess* proc = new QProcess();
connect(proc, SIGNAL(finished(int)), proc, SLOT(deleteLater()));
connect(proc, SIGNAL(finished(int)), <some_pointer>, SLOT(doSomething()));

I tried this and it seems to work but I don't know if it is the correct way to go. If it isn't, what is the best solution?
Thanks!

MarekR22
12th July 2011, 22:00
it is safe since deleteLater will have no effect until event loop kicks in (new event loops have no effect).
Note that by default slots are called synchronously, so before event loop start processing new events.
In general you should remember that order of calling slots (when invoke by signals) is undefined (you can't assume that you have a control who will get signal first).

Santosh Reddy
13th July 2011, 02:52
I tried this and it seems to work but I don't know if it is the correct way to go. If it isn't, what is the best solution?
It is safe to use, the way you mentioned, but it would more logical to connect the deleteLater() as the last connection, like this


QProcess* proc = new QProcess();
connect(proc, SIGNAL(finished(int)), <some_pointer_1>, SLOT(doSomething1()));
connect(proc, SIGNAL(finished(int)), <some_pointer_2>, SLOT(doSomething2()));
connect(proc, SIGNAL(finished(int)), <some_pointer_3>, SLOT(doSomething3()));
connect(proc, SIGNAL(finished(int)), proc, SLOT(deleteLater()));


In general you should remember that order of calling slots (when invoke by signals) is undefined (you can't assume that you have a control who will get signal first).
If a signal is connected to several slots, the slots are activated in the same order as the order the connection was made, when the signal is emitted.