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
427427
428428 void ScriptManager::executeOnScriptThread(std::function<void()> function, const Qt::ConnectionType& type ) {
429429 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+ });
431434 return;
432435 }
433436
@@ -859,10 +862,10 @@ void ScriptManager::removeEventHandler(const EntityItemID& entityID, const QStri · 425 unmodified lines
859862 qCDebug(scriptengine) << "*** WARNING *** ScriptManager::removeEventHandler() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] "
860863 "entityID:" << entityID << " eventName:" << eventName;
861864 #endif
862− QMetaObject::invokeMethod(this, "removeEventHandler",
863− Q_ARG(const EntityItemID&, entityID),
864− Q_ARG(const QString&, eventName),
865− Q_ARG(const ScriptValue&, handler));
865+ // Lambda is necessary there to keep shared_ptr counter above zero
866+ QMetaObject::invokeMethod(this, [=, manager = shared_from_this()]{
867+ manager->removeEventHandler(entityID, eventName, handler);
868+ });
866869 return;
867870 }
868871 #ifdef THREAD_DEBUGGING
@@ -906,10 +909,10 @@ void ScriptManager::addEventHandler(const EntityItemID& entityID, const QString& · 37 unmodified lines
906909 "entityID:" << entityID << " eventName:" << eventName;
907910 #endif
908911
909− QMetaObject::invokeMethod(this, "addEventHandler",
910− Q_ARG(const EntityItemID&, entityID),
911− Q_ARG(const QString&, eventName),
912− Q_ARG(const ScriptValue&, handler));
912+ // Lambda is necessary there to keep shared_ptr counter above zero
913+ QMetaObject::invokeMethod(this, [=, manager = shared_from_this()]{
914+ manager->addEventHandler(entityID, eventName, handler);
915+ });
913916 return;
914917 }
915918 #ifdef THREAD_DEBUGGING
@@ -1168,7 +1171,10 @@ void ScriptManager::stop(bool marshal) { · 252 unmodified lines
11681171 _isStopping = true; // this can be done on any thread
11691172
11701173 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+ });
11721178 return;
11731179 }
11741180
@@ -2012,11 +2018,10 @@ bool ScriptManager::hasEntityScriptDetails(const EntityItemID& entityID) const { · 837 unmodified lines
20122018
20132019 void ScriptManager::loadEntityScript(const EntityItemID& entityID, const QString& entityScript, bool forceRedownload) {
20142020 if (QThread::currentThread() != thread()) {
2015− QMetaObject::invokeMethod(this, "loadEntityScript",
2016− Q_ARG(const EntityItemID&, entityID),
2017− Q_ARG(const QString&, entityScript),
2018− Q_ARG(bool, forceRedownload)
2019− );
2021+ // Lambda is necessary there to keep shared_ptr counter above zero
2022+ QMetaObject::invokeMethod(this, [=, manager = shared_from_this()]{
2023+ manager->loadEntityScript(entityID, entityScript, forceRedownload);
2024+ });
20202025 return;
20212026 }
20222027 PROFILE_RANGE(script, __FUNCTION__);
@@ -2093,13 +2098,10 @@ void ScriptManager::entityScriptContentAvailable(const EntityItemID& entityID, c · 70 unmodified lines
20932098 << contents << "isURL:" << isURL << "success:" << success;
20942099 #endif
20952100
2096− QMetaObject::invokeMethod(this, "entityScriptContentAvailable",
2097− Q_ARG(const EntityItemID&, entityID),
2098− Q_ARG(const QString&, scriptOrURL),
2099− Q_ARG(const QString&, contents),
2100− Q_ARG(bool, isURL),
2101− Q_ARG(bool, success),
2102− Q_ARG(const QString&, status));
2101+ // Lambda is necessary there to keep shared_ptr counter above zero
2102+ QMetaObject::invokeMethod(this, [=, manager = shared_from_this()]{
2103+ manager->entityScriptContentAvailable(entityID, scriptOrURL, contents, isURL, success, status);
2104+ });
21032105 return;
21042106 }
21052107
@@ -2443,9 +2445,10 @@ void ScriptManager::unloadEntityScript(const EntityItemID& entityID, bool should · 337 unmodified lines
24432445 "entityID:" << entityID;
24442446 #endif
24452447
2446− QMetaObject::invokeMethod(this, "unloadEntityScript",
2447− Q_ARG(const EntityItemID&, entityID),
2448− Q_ARG(bool, shouldRemoveFromMap));
2448+ // Lambda is necessary there to keep shared_ptr counter above zero
2449+ QMetaObject::invokeMethod(this, [=, manager = shared_from_this()]{
2450+ manager->unloadEntityScript(entityID, shouldRemoveFromMap);
2451+ });
24492452 return;
24502453 }
24512454 #ifdef THREAD_DEBUGGING
@@ -2496,8 +2499,10 @@ void ScriptManager::unloadAllEntityScripts(bool blockingCall) { · 44 unmodified lines
24962499 qCDebug(scriptengine) << "*** WARNING *** ScriptManager::unloadAllEntityScripts() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "]";
24972500 #endif
24982501
2499− QMetaObject::invokeMethod(this, "unloadAllEntityScripts",
2500− blockingCall ? Qt::BlockingQueuedConnection : Qt::QueuedConnection);
2502+ // Lambda is necessary there to keep shared_ptr counter above zero
2503+ QMetaObject::invokeMethod(this, [=, manager = shared_from_this()]{
2504+ manager->unloadAllEntityScripts(blockingCall ? Qt::BlockingQueuedConnection : Qt::QueuedConnection);
2505+ });
25012506 return;
25022507 }
25032508 #ifdef THREAD_DEBUGGING
@@ -2590,12 +2595,10 @@ void ScriptManager::callEntityScriptMethod(const EntityItemID& entityID, const Q · 86 unmodified lines
25902595 qCDebug(scriptengine) << "*** WARNING *** ScriptManager::callEntityScriptMethod() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] "
25912596 "entityID:" << entityID << "methodName:" << methodName;
25922597 #endif
2593−
2594− QMetaObject::invokeMethod(this, "callEntityScriptMethod",
2595− Q_ARG(const EntityItemID&, entityID),
2596− Q_ARG(const QString&, methodName),
2597− Q_ARG(const QStringList&, params),
2598− Q_ARG(const QUuid&, remoteCallerID));
2598+ // Lambda is necessary there to keep shared_ptr counter above zero
2599+ QMetaObject::invokeMethod(this, [=, manager = shared_from_this()]{
2600+ manager->callEntityScriptMethod(entityID, methodName, params, remoteCallerID);
2601+ });
25992602 return;
26002603 }
26012604 #ifdef THREAD_DEBUGGING
@@ -2660,10 +2663,10 @@ void ScriptManager::callEntityScriptMethod(const EntityItemID& entityID, const Q · 58 unmodified lines
26602663 "entityID:" << entityID << "methodName:" << methodName << "event: mouseEvent";
26612664 #endif
26622665
2663− QMetaObject::invokeMethod(this, "callEntityScriptMethod",
2664− Q_ARG(const EntityItemID&, entityID),
2665− Q_ARG(const QString&, methodName),
2666− Q_ARG(const PointerEvent&, event));
2666+ // Lambda is necessary there to keep shared_ptr counter above zero
2667+ QMetaObject::invokeMethod(this, [=, manager = shared_from_this()]{
2668+ manager->callEntityScriptMethod(entityID, methodName, event);
2669+ });
26672670 return;
26682671 }
26692672 #ifdef THREAD_DEBUGGING
@@ -2699,11 +2702,10 @@ void ScriptManager::callEntityScriptMethod(const EntityItemID& entityID, const Q · 29 unmodified lines
26992702 "entityID:" << entityID << "methodName:" << methodName << "otherID:" << otherID << "collision: collision";
27002703 #endif
27012704
2702− QMetaObject::invokeMethod(this, "callEntityScriptMethod",
2703− Q_ARG(const EntityItemID&, entityID),
2704− Q_ARG(const QString&, methodName),
2705− Q_ARG(const EntityItemID&, otherID),
2706− Q_ARG(const Collision&, collision));
2705+ // Lambda is necessary there to keep shared_ptr counter above zero
2706+ QMetaObject::invokeMethod(this, [=, manager = shared_from_this()]{
2707+ manager->callEntityScriptMethod(entityID, methodName, otherID, collision);
2708+ });
27072709 return;
27082710 }
27092711 #ifdef THREAD_DEBUGGING