Fixed entity script reloads

This commit is contained in:
ksuprynowicz 2023-08-28 23:34:19 +02:00
parent 166f7223d1
commit 1ab46f68e3
4 changed files with 18 additions and 9 deletions

View file

@ -226,10 +226,7 @@ void EntityTreeRenderer::resetPersistentEntitiesScriptEngine() {
manager->stop();
manager->waitTillDoneRunning();
manager->disconnectNonEssentialSignals();
// TODO: script manager pointer is still in use somewhere after the cleanup in lambda.
// To prevent memory leaks on multiple reloads we would need to find all the usages and remove them.
// Script engines are correctly deleted later during shutdown currently.
qDebug() << "_nonPersistentEntitiesScriptManager lambda finished, script manager pointer use count: " << manager.use_count();
manager->removeFromScriptEngines();
});
}
_persistentEntitiesScriptManager = scriptManagerFactory(ScriptManager::ENTITY_CLIENT_SCRIPT, NO_SCRIPT,
@ -252,10 +249,7 @@ void EntityTreeRenderer::resetNonPersistentEntitiesScriptEngine() {
manager->stop();
manager->waitTillDoneRunning();
manager->disconnectNonEssentialSignals();
// TODO: script manager pointer is still in use somewhere after the cleanup in lambda.
// To prevent memory leaks on multiple reloads we would need to find all the usages and remove them.
// Script engines are correctly deleted later during shutdown currently.
qDebug() << "_nonPersistentEntitiesScriptManager lambda finished, script manager pointer use count: " << manager.use_count();
manager->removeFromScriptEngines();
});
}
_nonPersistentEntitiesScriptManager = scriptManagerFactory(ScriptManager::ENTITY_CLIENT_SCRIPT, NO_SCRIPT,

View file

@ -184,6 +184,8 @@ public:
void addScriptEngine(ScriptManagerPointer);
void removeScriptEngine(ScriptManagerPointer);
ScriptGatekeeper scriptGatekeeper;
signals:
@ -336,7 +338,6 @@ protected slots:
protected:
ScriptManagerPointer reloadScript(const QString& scriptName, bool isUserLoaded = true) { return loadScript(scriptName, isUserLoaded, false, false, true); }
void removeScriptEngine(ScriptManagerPointer);
void onScriptEngineLoaded(const QString& scriptFilename);
void quitWhenFinished();
void onScriptEngineError(const QString& scriptFilename);

View file

@ -519,6 +519,11 @@ void ScriptManager::waitTillDoneRunning(bool shutdown) {
}
}
void ScriptManager::removeFromScriptEngines() {
Q_ASSERT(_scriptEngines);
_scriptEngines.toStrongRef()->removeScriptEngine(shared_from_this());
}
QString ScriptManager::getFilename() const {
QStringList fileNameParts = _fileNameString.split("/");
QString lastPart;

View file

@ -471,6 +471,15 @@ public:
*/
void waitTillDoneRunning(bool shutdown = false);
/**
* @brief Removes shared pointer to script engine from the list of all script engines.
*
* This allows deletion of the script engine once all shared pointer instances are gone.
* This function is called for entity script engines when they are being destroyed.
*
*/
void removeFromScriptEngines();
/**
* @brief Load a script from a given URL
*