From c56bac4f9586bbfb4dc1b35a65717c50f8e7d257 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Mon, 16 Jun 2025 22:02:14 -0400 Subject: [PATCH] Fix Map::m_scriptFileWatcher being created before its needed --- CHANGELOG.md | 1 + src/core/map.cpp | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f6416511..8aa05f7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project somewhat adheres to [Semantic Versioning](https://semver.org/sp ## [Unreleased] ### Fixed - Fix warning not appearing when the log file exceeds maximum size. +- Fix unnecessary resources being used to watch files. ## [6.1.0] - 2025-06-09 ### Added diff --git a/src/core/map.cpp b/src/core/map.cpp index 6ced8d4c..ceb60f33 100644 --- a/src/core/map.cpp +++ b/src/core/map.cpp @@ -15,9 +15,6 @@ Map::Map(QObject *parent) : QObject(parent) { m_editHistory = new QUndoStack(this); - m_scriptFileWatcher = new QFileSystemWatcher(this); - connect(m_scriptFileWatcher, &QFileSystemWatcher::fileChanged, this, &Map::invalidateScripts); - resetEvents(); m_header = new MapHeader(this); @@ -143,7 +140,7 @@ void Map::setSharedScriptsMap(const QString &sharedScriptsMap) { void Map::invalidateScripts() { m_scriptsLoaded = false; - m_scriptFileWatcher->removePaths(m_scriptFileWatcher->files()); + delete m_scriptFileWatcher; emit scriptsModified(); } @@ -161,6 +158,12 @@ QStringList Map::getScriptLabels(Event::Group group) { m_loggedScriptsFileError = true; } + if (!m_scriptFileWatcher) { + // Only create the file watcher when it's first needed (even an empty QFileSystemWatcher will consume system resources). + // The other option would be for Porymap to have a single global QFileSystemWatcher, but that has complications of its own. + m_scriptFileWatcher = new QFileSystemWatcher(this); + connect(m_scriptFileWatcher, &QFileSystemWatcher::fileChanged, this, &Map::invalidateScripts); + } if (!m_scriptFileWatcher->files().contains(scriptsFilepath) && !m_scriptFileWatcher->addPath(scriptsFilepath) && !m_loggedScriptsFileError) { logWarn(QString("Failed to add scripts file '%1' to file watcher for %2.") .arg(Util::stripPrefix(scriptsFilepath, projectConfig.projectDir() + "/"))