History

switch to using QSharedPointer for _entitiesScriptEngine

Brad Hefta-Gaub committed 10 years ago

Commit 9db0fe01 parent a3221f34 files · +54 −23Browse files

Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 4 files). Show all

Unified · Split

libraries/script-engine/src/ScriptEngine.cpp+21−11
@@ -143,6 +143,7 @@ ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNam · 142 unmodified lines
143143
144144 ScriptEngine::~ScriptEngine() {
145145 qCDebug(scriptengine) << __FUNCTION__ << "--- BEGIN --- script:" << getFilename();
146+ qCDebug(scriptengine) << __FUNCTION__ << "called on thread[" << QThread::currentThread() << "], object's thread [" << thread() << "] script:" << getFilename();
146147 qCDebug(scriptengine) << "Script Engine shutting down (destructor) for script:" << getFilename();
147148
148149 auto scriptEngines = DependencyManager::get<ScriptEngines>();
@@ -1051,7 +1052,10 @@ void ScriptEngine::forwardHandlerCall(const EntityItemID& entityID, const QStrin · 902 unmodified lines
10511052
10521053 // since all of these operations can be asynch we will always do the actual work in the response handler
10531054 // for the download
1054−void ScriptEngine::loadEntityScript(const EntityItemID& entityID, const QString& entityScript, bool forceRedownload) {
1055+void ScriptEngine::loadEntityScript(QWeakPointer<ScriptEngine> theEngine, const EntityItemID& entityID, const QString& entityScript, bool forceRedownload) {
1056+
1057+
1058+/*****
10551059 if (QThread::currentThread() != thread()) {
10561060 #ifdef THREAD_DEBUGGING
10571061 qDebug() << "*** WARNING *** ScriptEngine::loadEntityScript() called on wrong thread ["
@@ -1070,25 +1074,30 @@ void ScriptEngine::loadEntityScript(const EntityItemID& entityID, const QString& · 12 unmodified lines
10701074 "entityID:" << entityID << "entityScript:" << entityScript << "forceRedownload:" << forceRedownload;
10711075 #endif
10721076
1073− // If we've been called our known entityScripts should not know about us..
1074− assert(!_entityScripts.contains(entityID));
1077+ // If we've been called our known entityScripts should not know about us..
1078+ assert(!_entityScripts.contains(entityID));
10751079
10761080 #ifdef THREAD_DEBUGGING
10771081 qDebug() << "ScriptEngine::loadEntityScript() calling scriptCache->getScriptContents() on thread ["
10781082 << QThread::currentThread() << "] expected thread [" << thread() << "]";
10791083 #endif
1084+******/
10801085
1081− QPointer<ScriptEngine> theEngine(this);
1082−
1086+ // NOTE: If the script content is not currently in the caceh, The LAMBDA here, will be called on the Main Thread
1087+ // which means we're guarenteed that it's not the correct thread for the ScriptEngine. This means
1088+ // when we get into entityScriptContentAvailable() we will likely invokeMethod() to get it over
1089+ // to the "Entities" ScriptEngine thread.
10831090 DependencyManager::get<ScriptCache>()->getScriptContents(entityScript, [theEngine, entityID](const QString& scriptOrURL, const QString& contents, bool isURL, bool success) {
1084−#ifdef THREAD_DEBUGGING
1085− qDebug() << "ScriptEngine::entityScriptContentAvailable() IN LAMBDA contentAvailable on thread ["
1086− << QThread::currentThread() << "] expected thread [" << thread() << "]";
1091+ QSharedPointer<ScriptEngine> strongEngine = theEngine.toStrongRef();
1092+ if (strongEngine) {
1093+ qDebug() << "ScriptCache::getScriptContents() returned ScriptEngine still active calling ... entityScriptContentAvailable()";
1094+#if 1//def THREAD_DEBUGGING
1095+ qDebug() << "ScriptEngine::entityScriptContentAvailable() IN LAMBDA contentAvailable on thread ["
1096+ << QThread::currentThread() << "] expected thread [" << strongEngine->thread() << "]";
10871097 #endif
10881098
1089− if (!theEngine.isNull()) {
1090− qDebug() << "ScriptCache::getScriptContents() returned ScriptEngine still active calling ... entityScriptContentAvailable()";
1091− theEngine->entityScriptContentAvailable(entityID, scriptOrURL, contents, isURL, success);
1099+
1100+ strongEngine->entityScriptContentAvailable(entityID, scriptOrURL, contents, isURL, success);
10921101 } else {
10931102 qDebug() << "ScriptCache::getScriptContents() returned after our ScriptEngine was deleted...";
10941103 }
@@ -1098,6 +1107,7 @@ void ScriptEngine::loadEntityScript(const EntityItemID& entityID, const QString& · 3 unmodified lines
10981107 // since all of these operations can be asynch we will always do the actual work in the response handler
10991108 // for the download
11001109 void ScriptEngine::entityScriptContentAvailable(const EntityItemID& entityID, const QString& scriptOrURL, const QString& contents, bool isURL, bool success) {
1110+
11011111 if (QThread::currentThread() != thread()) {
11021112 #ifdef THREAD_DEBUGGING
11031113 qDebug() << "*** WARNING *** ScriptEngine::entityScriptContentAvailable() called on wrong thread ["