Important: Please read the Qt Code of Conduct -

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
    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

  • How can i edit it?

  • bq. I assume, you do it inside run, right?

    No, this slot i called from other slot, which causes signal

  • This is my run()
    @void myThread::run()
    findNext() is function witch called connected slot and signal, and when slot was called, it called getDeleteAndQuit() as function

  • 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->wait(); // <-- wits till the thread has exited
    deleteLater(); // <-- ensures that it will be deleted later when the (main?) event loop executes again

  • It is help me!
    Thank you for your help!

Log in to reply