Qt Application crashes :QThread: Destroyed while thread is still running
-
wrote on 24 Jan 2022, 06:27 last edited by Vivek_A
Hi,
i have seen lot of question related to this ,but nothing that help that much ..
When i close my application , crashed pop up coming .. in debugger saying QThread: Destroyed while thread is still running.i referred link text,link text,stackoverflow
so i created following in my main.cpp
int main(int argc, char *argv[]) { WorkerObject* a = new WorkerObject(); RTLSControllerApplication app(argc, argv); app.mainWindow()->show(); QObject::connect(a, SIGNAL(finished()), &app, SLOT(quit())); return app.QApplication::exec(); }
i created worker thread and its connected from RTLSClient.cpp class signal.
in RTLSClient.cppRTLSClient::~RTLSClient() { workerThread.quit(); workerThread.wait(); }
still same problem !!
For more details - this is a continution of my previous question Update Data to mongodb through Qthread.. -
Hi,
i have seen lot of question related to this ,but nothing that help that much ..
When i close my application , crashed pop up coming .. in debugger saying QThread: Destroyed while thread is still running.i referred link text,link text,stackoverflow
so i created following in my main.cpp
int main(int argc, char *argv[]) { WorkerObject* a = new WorkerObject(); RTLSControllerApplication app(argc, argv); app.mainWindow()->show(); QObject::connect(a, SIGNAL(finished()), &app, SLOT(quit())); return app.QApplication::exec(); }
i created worker thread and its connected from RTLSClient.cpp class signal.
in RTLSClient.cppRTLSClient::~RTLSClient() { workerThread.quit(); workerThread.wait(); }
still same problem !!
For more details - this is a continution of my previous question Update Data to mongodb through Qthread..@Vivek_A
instead of
QObject::connect(a, SIGNAL(finished()), &app, SLOT(quit()));
I think you actually want this connect:
QObject::connect(&app, &QCoreApplication::aboutToQuit, a, &WorkerObject::quit);
-
@Vivek_A
instead of
QObject::connect(a, SIGNAL(finished()), &app, SLOT(quit()));
I think you actually want this connect:
QObject::connect(&app, &QCoreApplication::aboutToQuit, a, &WorkerObject::quit);
wrote on 24 Jan 2022, 09:39 last edited by@J-Hilk sry , there is one doubt ,
i created Qthread variable in RTLSClient.h file and moving worker object to this thread..QObject::connect(&app, &QCoreApplication::aboutToQuit, a, &WorkerObject::quit);
This saying quit not a member of WorkerObject.
so i created a slot (quit ) in WorkerObject for calling thread quit and wait .. here is my doubt Qthread variable is in RTLSClient class right .......?????? -
@J-Hilk sry , there is one doubt ,
i created Qthread variable in RTLSClient.h file and moving worker object to this thread..QObject::connect(&app, &QCoreApplication::aboutToQuit, a, &WorkerObject::quit);
This saying quit not a member of WorkerObject.
so i created a slot (quit ) in WorkerObject for calling thread quit and wait .. here is my doubt Qthread variable is in RTLSClient class right .......??????@Vivek_A
you only gave me this to work withint main(int argc, char *argv[]) { WorkerObject* a = new WorkerObject(); RTLSControllerApplication app(argc, argv); app.mainWindow()->show(); QObject::connect(a, SIGNAL(finished()), &app, SLOT(quit())); return app.QApplication::exec(); }
There is no mention of QThread instance, or an RTLSClient instance nor do I see the moveToThread call
So my example code is based on guess work.
You have to show some more context for me to adapt the example code,
but, you're right, the signal
aboutToQuit
from your QCoreApplication (qApp) has to be connected to the slotquit
of the QThread instance -
@Vivek_A
you only gave me this to work withint main(int argc, char *argv[]) { WorkerObject* a = new WorkerObject(); RTLSControllerApplication app(argc, argv); app.mainWindow()->show(); QObject::connect(a, SIGNAL(finished()), &app, SLOT(quit())); return app.QApplication::exec(); }
There is no mention of QThread instance, or an RTLSClient instance nor do I see the moveToThread call
So my example code is based on guess work.
You have to show some more context for me to adapt the example code,
but, you're right, the signal
aboutToQuit
from your QCoreApplication (qApp) has to be connected to the slotquit
of the QThread instancewrote on 25 Jan 2022, 07:09 last edited by Vivek_A@J-Hilk sorry for not detailing ,,,
workerobject.h
#include <QObject> #include <QThread> class WorkerObject : public QObject { Q_OBJECT public: explicit WorkerObject(QObject *parent = nullptr); public slots: void heavyOperation(int ,double ,double); void quit(); signals: void finished(); private: };
RTLSClient.h
class RTLSClient : public QObject { Q_OBJECT QThread workerThread; public: explicit RTLSClient(QObject *parent = 0); virtual ~RTLSClient(); //and some signal and slots .......
***RTLSClient.cpp***
RTLSClient.cpp
WorkerObject *worker = new WorkerObject; worker->moveToThread(&workerThread); connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater); connect(this, &RTLSClient::sendtodb, worker, &WorkerObject::heavyOperation); connect(worker, &WorkerObject::finished, &workerThread, &QThread::quit); connect(worker, &WorkerObject::finished, worker, &QObject::deleteLater); RTLSClient::~RTLSClient() { workerThread.quit(); workerThread.wait(); if(file != nullptr) file->close(); }
workersobject.cpp
WorkerObject::WorkerObject(QObject *parent) : QObject(parent) { } void WorkerObject::heavyOperation(int a ,double x,double y) { dbx["Tag2"].delete_one(document{} << "bool" << 1 << finalize); }
THESE are the main code segments iam make use of threading ...
can you find what is the mistake and how to quit thread safely. -
@Vivek_A said in Qt Application crashes :QThread: Destroyed while thread is still running:
RTLSClient
Where do you create/delete this object? If you don't delete it then the dtor will not be called and your thread will not be stopped.
-
@Vivek_A said in Qt Application crashes :QThread: Destroyed while thread is still running:
RTLSClient
Where do you create/delete this object? If you don't delete it then the dtor will not be called and your thread will not be stopped.
wrote on 25 Jan 2022, 08:13 last edited by Vivek_A@Christian-Ehrlicher its created in main thread-
RTLSControllerApplication::RTLSControllerApplication(int &argc, char **argv) : QApplication(argc, argv) { _cleConnection = new RTLSCLEConnection(this); _control = new RTLSControl(this); _client = new RTLSClient(this); _mainWindow = new MainWindow(); // and its connections here ........ } RTLSControllerApplication::~RTLSControllerApplication() { // Delete the objects manually, because we want to control the order delete _mainWindow; delete _client; delete _control; delete _cleConnection; delete _viewSettings; delete _anchorSelectionModel; delete _model; }
RTLSControllerApplication its the main thread ...you can see its called from main function.above
-
Please run your app with the env var QT_FATAL_WARNINGS and take a look at the backtrace to see where the error comes from.
Also make a breakpoint in your RTLSClient dtor to see if it is reached. -
Please run your app with the env var QT_FATAL_WARNINGS and take a look at the backtrace to see where the error comes from.
Also make a breakpoint in your RTLSClient dtor to see if it is reached.wrote on 7 Feb 2022, 11:57 last edited by Vivek_A 2 Jul 2022, 12:14I changed the destructor code of RTLSClient to
RTLSClient::~RTLSClient() { workerThread.terminate(); if(!workerThread.wait(3000)) //Wait until it actually has terminated (max. 3 sec) { workerThread.terminate(); //Thread didn't exit in time, probably deadlocked, terminate it! workerThread.wait(); //We have to wait again here! } }
now app not showing ..QThread: Destroyed while thread is still running but it not responding when opening