PDA

View Full Version : How to completely kill a QThread



Qting
11th February 2012, 10:03
Hi,

I have a GUI application and I am using QThread to run some processing in the background. The GUI application communicates with the Qthread through signals. I need to completely kill the Qthread and free up its memory once the background processing is over. I thought that once I call the Qthread::exit(0) inside the Qthread it would get killed. But even after this it continues to respond to the Signals sent by the GUI application. This means that it does not get killed and is still consuming the memory.
Somebody please help. I am creating the thread as follows, Location_Updates is inheriting from Qthread

Location_Updates *updates;
updates = new Location_Updates(this);
updates->start();

MarekR22
11th February 2012, 19:46
exit works when you are using event loop!
if your run method doesn't invoke exec() (event loop) exit will have no effect.
Apparently you have sub-classed QThread and you shouldn't do it.
Read this http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/

Qting
12th February 2012, 08:16
i am calling exec() inside the run() method of thread. Should the thread cease to exist once exit is called? I try to delete it using deleteLater() but it says thread panicked.

wysota
12th February 2012, 12:45
Don't confuse QThread object and a thread it represents. Those are two totally different entities. The thread ceases to exist when QThread::run() function returns, the only thing that remains is the QThread object which is a similar object to QTimer, QObject or QComboBox governed by exactly the same rules.

Qting
12th February 2012, 18:56
So, what do I have to do to completely free the memory consumed by the Thread and the objects. When I try to use deleteLater() slot, the app crashes with message as - Thread panicked. I need some way to make the thread completely cease to exist.

wysota
12th February 2012, 19:32
Stop the thread and when it signals that it has finished(), destroy the QThread object.