History

Fix script-related crashes on exiting domain

Karol Suprynowicz committed 1 year ago

Commit 9e261461 parent 9503fae3 files · +61 −50Browse files

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

Unified · Split

libraries/script-engine/src/ScriptManager.cpp+44−42
@@ -427,7 +427,10 @@ void ScriptManager::runInThread() { · 426 unmodified lines
427 427
428void ScriptManager::executeOnScriptThread(std::function<void()> function, const Qt::ConnectionType& type ) {428void ScriptManager::executeOnScriptThread(std::function<void()> function, const Qt::ConnectionType& type ) {
429 if (QThread::currentThread() != thread()) {429 if (QThread::currentThread() != thread()) {
430 QMetaObject::invokeMethod(this, "executeOnScriptThread", type, Q_ARG(std::function<void()>, function));430 // Lambda is necessary there to keep shared_ptr counter above zero
431 QMetaObject::invokeMethod(this, [=, manager = shared_from_this()]{
432 manager->executeOnScriptThread(function, type);
433 });
431 return;434 return;
432 }435 }
433 436
@@ -859,10 +862,10 @@ void ScriptManager::removeEventHandler(const EntityItemID& entityID, const QStri · 425 unmodified lines
859 qCDebug(scriptengine) << "*** WARNING *** ScriptManager::removeEventHandler() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] "862 qCDebug(scriptengine) << "*** WARNING *** ScriptManager::removeEventHandler() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] "
860 "entityID:" << entityID << " eventName:" << eventName;863 "entityID:" << entityID << " eventName:" << eventName;
861#endif864#endif
862 QMetaObject::invokeMethod(this, "removeEventHandler",865 // Lambda is necessary there to keep shared_ptr counter above zero
863 Q_ARG(const EntityItemID&, entityID),866 QMetaObject::invokeMethod(this, [=, manager = shared_from_this()]{
864 Q_ARG(const QString&, eventName),867 manager->removeEventHandler(entityID, eventName, handler);
865 Q_ARG(const ScriptValue&, handler));868 });
866 return;869 return;
867 }870 }
868#ifdef THREAD_DEBUGGING871#ifdef THREAD_DEBUGGING
@@ -906,10 +909,10 @@ void ScriptManager::addEventHandler(const EntityItemID& entityID, const QString& · 37 unmodified lines
906 "entityID:" << entityID << " eventName:" << eventName;909 "entityID:" << entityID << " eventName:" << eventName;
907#endif910#endif
908 911
909 QMetaObject::invokeMethod(this, "addEventHandler",912 // Lambda is necessary there to keep shared_ptr counter above zero
910 Q_ARG(const EntityItemID&, entityID),913 QMetaObject::invokeMethod(this, [=, manager = shared_from_this()]{
911 Q_ARG(const QString&, eventName),914 manager->addEventHandler(entityID, eventName, handler);
912 Q_ARG(const ScriptValue&, handler));915 });
913 return;916 return;
914 }917 }
915#ifdef THREAD_DEBUGGING918#ifdef THREAD_DEBUGGING
@@ -1168,7 +1171,10 @@ void ScriptManager::stop(bool marshal) { · 252 unmodified lines
1168 _isStopping = true; // this can be done on any thread1171 _isStopping = true; // this can be done on any thread
1169 1172
1170 if (marshal) {1173 if (marshal) {
1171 QMetaObject::invokeMethod(this, "stop");1174 // Lambda is necessary there to keep shared_ptr counter above zero if this gets called from different thread
1175 QMetaObject::invokeMethod(this, [=, manager = shared_from_this()]{
1176 manager->stop(false);
1177 });
1172 return;1178 return;
1173 }1179 }
1174 1180
@@ -2012,11 +2018,10 @@ bool ScriptManager::hasEntityScriptDetails(const EntityItemID& entityID) const { · 837 unmodified lines
2012 2018
2013void ScriptManager::loadEntityScript(const EntityItemID& entityID, const QString& entityScript, bool forceRedownload) {2019void ScriptManager::loadEntityScript(const EntityItemID& entityID, const QString& entityScript, bool forceRedownload) {
2014 if (QThread::currentThread() != thread()) {2020 if (QThread::currentThread() != thread()) {
2015 QMetaObject::invokeMethod(this, "loadEntityScript",2021 // Lambda is necessary there to keep shared_ptr counter above zero
2016 Q_ARG(const EntityItemID&, entityID),2022 QMetaObject::invokeMethod(this, [=, manager = shared_from_this()]{
2017 Q_ARG(const QString&, entityScript),2023 manager->loadEntityScript(entityID, entityScript, forceRedownload);
2018 Q_ARG(bool, forceRedownload)2024 });
2019 );
2020 return;2025 return;
2021 }2026 }
2022 PROFILE_RANGE(script, __FUNCTION__);2027 PROFILE_RANGE(script, __FUNCTION__);
@@ -2093,13 +2098,10 @@ void ScriptManager::entityScriptContentAvailable(const EntityItemID& entityID, c · 70 unmodified lines
2093 << contents << "isURL:" << isURL << "success:" << success;2098 << contents << "isURL:" << isURL << "success:" << success;
2094#endif2099#endif
2095 2100
2096 QMetaObject::invokeMethod(this, "entityScriptContentAvailable",2101 // Lambda is necessary there to keep shared_ptr counter above zero
2097 Q_ARG(const EntityItemID&, entityID),2102 QMetaObject::invokeMethod(this, [=, manager = shared_from_this()]{
2098 Q_ARG(const QString&, scriptOrURL),2103 manager->entityScriptContentAvailable(entityID, scriptOrURL, contents, isURL, success, status);
2099 Q_ARG(const QString&, contents),2104 });
2100 Q_ARG(bool, isURL),
2101 Q_ARG(bool, success),
2102 Q_ARG(const QString&, status));
2103 return;2105 return;
2104 }2106 }
2105 2107
@@ -2443,9 +2445,10 @@ void ScriptManager::unloadEntityScript(const EntityItemID& entityID, bool should · 337 unmodified lines
2443 "entityID:" << entityID;2445 "entityID:" << entityID;
2444#endif2446#endif
2445 2447
2446 QMetaObject::invokeMethod(this, "unloadEntityScript",2448 // Lambda is necessary there to keep shared_ptr counter above zero
2447 Q_ARG(const EntityItemID&, entityID),2449 QMetaObject::invokeMethod(this, [=, manager = shared_from_this()]{
2448 Q_ARG(bool, shouldRemoveFromMap));2450 manager->unloadEntityScript(entityID, shouldRemoveFromMap);
2451 });
2449 return;2452 return;
2450 }2453 }
2451#ifdef THREAD_DEBUGGING2454#ifdef THREAD_DEBUGGING
@@ -2496,8 +2499,10 @@ void ScriptManager::unloadAllEntityScripts(bool blockingCall) { · 44 unmodified lines
2496 qCDebug(scriptengine) << "*** WARNING *** ScriptManager::unloadAllEntityScripts() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "]";2499 qCDebug(scriptengine) << "*** WARNING *** ScriptManager::unloadAllEntityScripts() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "]";
2497#endif2500#endif
2498 2501
2499 QMetaObject::invokeMethod(this, "unloadAllEntityScripts",2502 // Lambda is necessary there to keep shared_ptr counter above zero
2500 blockingCall ? Qt::BlockingQueuedConnection : Qt::QueuedConnection);2503 QMetaObject::invokeMethod(this, [=, manager = shared_from_this()]{
2504 manager->unloadAllEntityScripts(blockingCall ? Qt::BlockingQueuedConnection : Qt::QueuedConnection);
2505 });
2501 return;2506 return;
2502 }2507 }
2503#ifdef THREAD_DEBUGGING2508#ifdef THREAD_DEBUGGING
@@ -2590,12 +2595,10 @@ void ScriptManager::callEntityScriptMethod(const EntityItemID& entityID, const Q · 86 unmodified lines
2590 qCDebug(scriptengine) << "*** WARNING *** ScriptManager::callEntityScriptMethod() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] "2595 qCDebug(scriptengine) << "*** WARNING *** ScriptManager::callEntityScriptMethod() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] "
2591 "entityID:" << entityID << "methodName:" << methodName;2596 "entityID:" << entityID << "methodName:" << methodName;
2592#endif2597#endif
2593 2598 // Lambda is necessary there to keep shared_ptr counter above zero
2594 QMetaObject::invokeMethod(this, "callEntityScriptMethod",2599 QMetaObject::invokeMethod(this, [=, manager = shared_from_this()]{
2595 Q_ARG(const EntityItemID&, entityID),2600 manager->callEntityScriptMethod(entityID, methodName, params, remoteCallerID);
2596 Q_ARG(const QString&, methodName),2601 });
2597 Q_ARG(const QStringList&, params),
2598 Q_ARG(const QUuid&, remoteCallerID));
2599 return;2602 return;
2600 }2603 }
2601#ifdef THREAD_DEBUGGING2604#ifdef THREAD_DEBUGGING
@@ -2660,10 +2663,10 @@ void ScriptManager::callEntityScriptMethod(const EntityItemID& entityID, const Q · 58 unmodified lines
2660 "entityID:" << entityID << "methodName:" << methodName << "event: mouseEvent";2663 "entityID:" << entityID << "methodName:" << methodName << "event: mouseEvent";
2661#endif2664#endif
2662 2665
2663 QMetaObject::invokeMethod(this, "callEntityScriptMethod",2666 // Lambda is necessary there to keep shared_ptr counter above zero
2664 Q_ARG(const EntityItemID&, entityID),2667 QMetaObject::invokeMethod(this, [=, manager = shared_from_this()]{
2665 Q_ARG(const QString&, methodName),2668 manager->callEntityScriptMethod(entityID, methodName, event);
2666 Q_ARG(const PointerEvent&, event));2669 });
2667 return;2670 return;
2668 }2671 }
2669#ifdef THREAD_DEBUGGING2672#ifdef THREAD_DEBUGGING
@@ -2699,11 +2702,10 @@ void ScriptManager::callEntityScriptMethod(const EntityItemID& entityID, const Q · 29 unmodified lines
2699 "entityID:" << entityID << "methodName:" << methodName << "otherID:" << otherID << "collision: collision";2702 "entityID:" << entityID << "methodName:" << methodName << "otherID:" << otherID << "collision: collision";
2700#endif2703#endif
2701 2704
2702 QMetaObject::invokeMethod(this, "callEntityScriptMethod",2705 // Lambda is necessary there to keep shared_ptr counter above zero
2703 Q_ARG(const EntityItemID&, entityID),2706 QMetaObject::invokeMethod(this, [=, manager = shared_from_this()]{
2704 Q_ARG(const QString&, methodName),2707 manager->callEntityScriptMethod(entityID, methodName, otherID, collision);
2705 Q_ARG(const EntityItemID&, otherID),2708 });
2706 Q_ARG(const Collision&, collision));
2707 return;2709 return;
2708 }2710 }
2709#ifdef THREAD_DEBUGGING2711#ifdef THREAD_DEBUGGING