properly connect signals and slots to shut down script properly, fix null termination in script file load

This commit is contained in:
ZappoMan 2013-12-14 14:23:33 -08:00
parent dacaade9ed
commit a220804fb0
3 changed files with 16 additions and 9 deletions

View file

@ -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);

View file

@ -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();
}

View file

@ -33,7 +33,6 @@ public slots:
void run();
void stop() {
_isFinished = true;
emit finished();
}
signals: