Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 1 file). Show all
Unified · Split
| @@ -107,6 +107,7 @@ ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNam · 106 unmodified lines | |||
| 107 | } | 107 | } |
| 108 | 108 | ||
| 109 | ScriptEngine::~ScriptEngine() { | 109 | ScriptEngine::~ScriptEngine() { |
| 110 | qDebug() << "ScriptEngine::~ScriptEngine().... this:" << this << "my thread:" << thread(); | ||
| 110 | // If we're not already in the middle of stopping all scripts, then we should remove ourselves | 111 | // If we're not already in the middle of stopping all scripts, then we should remove ourselves |
| 111 | // from the list of running scripts. We don't do this if we're in the process of stopping all scripts | 112 | // from the list of running scripts. We don't do this if we're in the process of stopping all scripts |
| 112 | // because that method removes scripts from its list as it iterates them | 113 | // because that method removes scripts from its list as it iterates them |
| @@ -117,20 +118,28 @@ ScriptEngine::~ScriptEngine() { · 4 unmodified lines | |||
| 117 | } | 118 | } |
| 118 | } | 119 | } |
| 119 | 120 | ||
| 121 | class MyWorkerThread : public QThread { | ||
| 122 | public: | ||
| 123 | MyWorkerThread(QObject* parent = nullptr) : QThread(parent) { qDebug() << "MyWorkerThread::MyWorkerThread() this:" << this; } | ||
| 124 | ~MyWorkerThread() { qDebug() << "MyWorkerThread::~MyWorkerThread() this:" << this; } | ||
| 125 | }; | ||
| 126 | |||
| 120 | void ScriptEngine::runInThread() { | 127 | void ScriptEngine::runInThread() { |
| 121 | QThread* workerThread = new QThread(this); | 128 | QThread* workerThread = new MyWorkerThread(); // thread is owned but ScriptEngine, so if the ScriptEngine is destroyed, the thread will be too. |
| 122 | QString scriptEngineName = QString("Script Thread:") + getFilename(); | 129 | QString scriptEngineName = QString("Script Thread:") + getFilename(); |
| 123 | workerThread->setObjectName(scriptEngineName); | 130 | workerThread->setObjectName(scriptEngineName); |
| 124 | 131 | ||
| 125 | // when the worker thread is started, call our engine's run.. | 132 | // when the worker thread is started, call our engine's run.. |
| 126 | connect(workerThread, &QThread::started, this, &ScriptEngine::run); | 133 | connect(workerThread, &QThread::started, this, &ScriptEngine::run); |
| 127 | 134 | ||
| 128 | // when the thread is terminated, add both scriptEngine and thread to the deleteLater queue | 135 | // tell the thread to stop when the script engine is done |
| 129 | connect(this, &ScriptEngine::doneRunning, this, &ScriptEngine::deleteLater); | 136 | connect(this, &ScriptEngine::doneRunning, workerThread, &QThread::quit); |
| 137 | |||
| 138 | // when the thread is finished, add thread to the deleteLater queue | ||
| 130 | connect(workerThread, &QThread::finished, workerThread, &QThread::deleteLater); | 139 | connect(workerThread, &QThread::finished, workerThread, &QThread::deleteLater); |
| 131 | 140 | ||
| 132 | // tell the thread to stop when the script engine is done | 141 | // when the thread is destroyed, add scriptEngine to the deleteLater queue |
| 133 | connect(this, &ScriptEngine::destroyed, workerThread, &QThread::quit); | 142 | connect(workerThread, &QThread::finished, this, &ScriptEngine::deleteLater); |
| 134 | 143 | ||
| 135 | moveToThread(workerThread); | 144 | moveToThread(workerThread); |
| 136 | 145 | ||
| @@ -650,10 +659,12 @@ void ScriptEngine::run() { · 513 unmodified lines | |||
| 650 | _isRunning = false; | 659 | _isRunning = false; |
| 651 | if (_wantSignals) { | 660 | if (_wantSignals) { |
| 652 | emit runningStateChanged(); | 661 | emit runningStateChanged(); |
| 662 | qDebug() << "ScriptEngine::run().... about to emit doneRunning().... this:" << this << "my thread:" << thread() << "current thread:" << QThread::currentThread(); | ||
| 653 | emit doneRunning(); | 663 | emit doneRunning(); |
| 654 | } | 664 | } |
| 655 | 665 | ||
| 656 | _doneRunningThisScript = true; | 666 | _doneRunningThisScript = true; |
| 667 | qDebug() << "ScriptEngine::run().... END OF RUN.... this:" << this << "my thread:" << thread() << "current thread:" << QThread::currentThread(); | ||
| 657 | } | 668 | } |
| 658 | 669 | ||
| 659 | // NOTE: This is private because it must be called on the same thread that created the timers, which is why | 670 | // NOTE: This is private because it must be called on the same thread that created the timers, which is why |