History

fixes leak of GenericThread and also fixes crash in octree servers on client disconnect

Brad Hefta-Gaub committed 12 years ago

Commit f97ab1d1 parent b4c963b4 files · +29 −21Browse files

Showing the changes to libraries/shared/src/GenericThread.cpp (1 of 4 files). Show all

Unified · Split

libraries/shared/src/GenericThread.cpp+14−8
@@ -8,8 +8,11 @@ · 7 unmodified lines
88 // Generic Threaded or non-threaded processing class
99 //
1010
11+#include <QDebug>
12+
1113 #include "GenericThread.h"
1214
15+
1316 GenericThread::GenericThread() :
1417 _stopThread(false),
1518 _isThreaded(false) // assume non-threaded, must call initialize()
@@ -23,15 +26,11 @@ GenericThread::~GenericThread() { · 7 unmodified lines
2326 void GenericThread::initialize(bool isThreaded) {
2427 _isThreaded = isThreaded;
2528 if (_isThreaded) {
26− QThread* _thread = new QThread(this);
29+ _thread = new QThread(this);
2730
2831 // when the worker thread is started, call our engine's run..
2932 connect(_thread, SIGNAL(started()), this, SLOT(threadRoutine()));
3033
31− // XXXBHG: this is a known memory leak/thread leak. I will fix this shortly.
32− //connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));
33− //connect(_thread, SIGNAL(finished()), _thread, SLOT(deleteLater()));
34−
3534 this->moveToThread(_thread);
3635
3736 // Starts an event loop, and emits _thread->started()
@@ -42,7 +41,11 @@ void GenericThread::initialize(bool isThreaded) { · 4 unmodified lines
4241 void GenericThread::terminate() {
4342 if (_isThreaded) {
4443 _stopThread = true;
45− //_isThreaded = false;
44+
45+ if (_thread) {
46+ _thread->wait();
47+ _thread->deleteLater();
48+ }
4649 }
4750 }
4851
@@ -61,7 +64,10 @@ void GenericThread::threadRoutine() { · 12 unmodified lines
6164 }
6265 }
6366
64− if (_isThreaded) {
65− emit finished();
67+ // If we were on a thread, then wait till it's done
68+ if (_isThreaded && _thread) {
69+ _thread->quit();
6670 }
71+
72+ emit finished();
6773 }