Quit and delete thread
-
Hellow, i have one problem:
I have many threads and when i finished thread i want to delete it, but if i write
@void myThread::getDeleteAndQuit() //slot
{
this->exit(0);
delete this;
}@
and coll it that i have error, why?
I dont use some slots and everything should be done linearly
I have no way of storing pointers to the threads
Advance many thanks for your help! -
You delete your self. I assume, you do it inside run, right? but run is only a method, called by QThread methods. after the delete, the call stack returns to the calling function (e.g. run) and the to the caller of run, which might access members of QThread, which you deleted.
EDIT: exit justs exits, the event loop
-
Now I get confused. Who calls what when?
exit will cause the event loop to stop, but not synchronously. The thread has not finished then.
perhaps the wait trick helps you:@
void myThread::getDeleteAndQuit() //slot
{
this->exit(0);
this->wait(); // <-- wits till the thread has exited
deleteLater(); // <-- ensures that it will be deleted later when the (main?) event loop executes again
}
@