From a220804fb0d22e807fa82c8a810f7830c0674c46 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Sat, 14 Dec 2013 14:23:33 -0800 Subject: [PATCH] properly connect signals and slots to shut down script properly, fix null termination in script file load --- interface/src/Application.cpp | 19 +++++++++++++------ libraries/scriptengine/src/ScriptEngine.cpp | 5 +++-- libraries/scriptengine/src/ScriptEngine.h | 1 - 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 89170f94a6..0804b3522c 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -4416,10 +4416,11 @@ void Application::loadScript() { file.seekg( 0, std::ios::beg ); // read the entire file into a buffer, WHAT!? Why not. - char* entireFile = new char[fileLength]; + char* entireFile = new char[fileLength+1]; file.read((char*)entireFile, fileLength); file.close(); + entireFile[fileLength] = 0;// null terminate QString script(entireFile); delete[] entireFile; @@ -4435,13 +4436,19 @@ void Application::loadScript() { //_scriptEngine->getParticleScriptingInterface()->setJurisdictionListener(); QThread* workerThread = new QThread(this); - + + // when the worker thread is started, call our engine's run.. connect(workerThread, SIGNAL(started()), _scriptEngine, SLOT(run())); - //connect(_scriptEngine, SIGNAL(finished()), this, SLOT(assignmentCompleted())); - //connect(_scriptEngine, SIGNAL(finished()), workerThread, SLOT(quit())); - //connect(_scriptEngine, SIGNAL(finished()), _scriptEngine, SLOT(deleteLater())); - //connect(_scriptEngine, SIGNAL(finished()), workerThread, SLOT(deleteLater())); + // when the engine emits finished, call our threads quit + connect(_scriptEngine, SIGNAL(finished()), workerThread, SLOT(quit())); + + // when the thread is terminated, add both _scriptEngine and thread to the deleteLater queue + connect(workerThread, SIGNAL(terminated()), _scriptEngine, SLOT(deleteLater())); + connect(workerThread, SIGNAL(terminated()), workerThread, SLOT(deleteLater())); + + // when the application is about to quit, stop our script engine so it unwinds properly + connect(this, SIGNAL(aboutToQuit()), _scriptEngine, SLOT(stop())); _scriptEngine->moveToThread(workerThread); diff --git a/libraries/scriptengine/src/ScriptEngine.cpp b/libraries/scriptengine/src/ScriptEngine.cpp index d601ba6287..0c486cb30f 100644 --- a/libraries/scriptengine/src/ScriptEngine.cpp +++ b/libraries/scriptengine/src/ScriptEngine.cpp @@ -52,6 +52,8 @@ void ScriptEngine::run() { // let the VoxelPacketSender know how frequently we plan to call it _voxelScriptingInterface.getVoxelPacketSender()->setProcessCallIntervalHint(VISUAL_DATA_CALLBACK_USECS); _particleScriptingInterface.getParticlePacketSender()->setProcessCallIntervalHint(VISUAL_DATA_CALLBACK_USECS); + + qDebug() << "Script:\n" << _scriptContents << "\n"; QScriptValue result = engine.evaluate(_scriptContents); qDebug() << "Evaluated script.\n"; @@ -112,7 +114,6 @@ void ScriptEngine::run() { int line = engine.uncaughtExceptionLineNumber(); qDebug() << "Uncaught exception at line" << line << ":" << engine.uncaughtException().toString() << "\n"; } - - } + emit finished(); } diff --git a/libraries/scriptengine/src/ScriptEngine.h b/libraries/scriptengine/src/ScriptEngine.h index 725c920a8a..9be9a5e0c0 100644 --- a/libraries/scriptengine/src/ScriptEngine.h +++ b/libraries/scriptengine/src/ScriptEngine.h @@ -33,7 +33,6 @@ public slots: void run(); void stop() { _isFinished = true; - emit finished(); } signals: