From c3daad6553dae68c0f743815d797e6b1868988d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Wed, 19 Jan 2022 23:36:12 -0300 Subject: [PATCH] ecode: Crash fix when loading a new folder when the previous folder is still loading. --- src/tools/codeeditor/codeeditor.cpp | 27 ++++++++++++++++++++------- src/tools/codeeditor/codeeditor.hpp | 1 + 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/tools/codeeditor/codeeditor.cpp b/src/tools/codeeditor/codeeditor.cpp index 37f1ba657..be1c21a19 100644 --- a/src/tools/codeeditor/codeeditor.cpp +++ b/src/tools/codeeditor/codeeditor.cpp @@ -1100,6 +1100,7 @@ void App::onDocumentLoaded( UICodeEditor* editor, const std::string& path ) { if ( mFileWatcher && doc.hasFilepath() && ( !mDirTree || !mDirTree->isDirInTree( doc.getFileInfo().getFilepath() ) ) ) { std::string dir( FileSystem::fileRemoveFileName( doc.getFileInfo().getFilepath() ) ); + Lock l( mWatchesLock ); mFilesFolderWatches[dir] = mFileWatcher->addWatch( dir, mFileSystemListener ); } } @@ -1419,6 +1420,7 @@ void App::onCodeEditorCreated( UICodeEditor* editor, TextDocument& doc ) { return; const DocEvent* docEvent = static_cast( event ); std::string dir( FileSystem::fileRemoveFileName( docEvent->getDoc()->getFilePath() ) ); + Lock l( mWatchesLock ); auto itWatch = mFilesFolderWatches.find( dir ); if ( mFileWatcher && itWatch != mFilesFolderWatches.end() ) { if ( !mDirTree || !mDirTree->isDirInTree( dir ) ) { @@ -1664,13 +1666,21 @@ void App::updateEditorState() { void App::removeFolderWatches() { if ( mFileWatcher ) { - for ( const auto& dir : mFolderWatches ) - mFileWatcher->removeWatch( dir ); - mFolderWatches.clear(); + std::unordered_set folderWatches; + std::unordered_map filesFolderWatches; + { + Lock l( mWatchesLock ); + folderWatches = mFolderWatches; + filesFolderWatches = mFilesFolderWatches; + mFolderWatches.clear(); + mFilesFolderWatches.clear(); + } - for ( const auto& fileFolder : mFilesFolderWatches ) + for ( const auto& dir : folderWatches ) + mFileWatcher->removeWatch( dir ); + + for ( const auto& fileFolder : filesFolderWatches ) mFileWatcher->removeWatch( fileFolder.second ); - mFilesFolderWatches.clear(); } } @@ -1691,8 +1701,11 @@ void App::loadDirTree( const std::string& path ) { } ); if ( mFileWatcher ) { removeFolderWatches(); - mFolderWatches.insert( - mFileWatcher->addWatch( dirTree.getPath(), mFileSystemListener, true ) ); + { + Lock l( mWatchesLock ); + mFolderWatches.insert( + mFileWatcher->addWatch( dirTree.getPath(), mFileSystemListener, true ) ); + } mFileSystemListener->setDirTree( mDirTree ); } }, diff --git a/src/tools/codeeditor/codeeditor.hpp b/src/tools/codeeditor/codeeditor.hpp index 9aa1682f9..6511a370a 100644 --- a/src/tools/codeeditor/codeeditor.hpp +++ b/src/tools/codeeditor/codeeditor.hpp @@ -115,6 +115,7 @@ class App : public UICodeEditorSplitter::Client { FontTrueType* mFontMono{ nullptr }; efsw::FileWatcher* mFileWatcher{ nullptr }; FileSystemListener* mFileSystemListener{ nullptr }; + Mutex mWatchesLock; std::unordered_set mFolderWatches; std::unordered_map mFilesFolderWatches; std::unique_ptr mGlobalSearchController;