diff --git a/radiantcore/decl/DeclarationManager.cpp b/radiantcore/decl/DeclarationManager.cpp index 3ddf30b79b..1c2d0e5f9c 100644 --- a/radiantcore/decl/DeclarationManager.cpp +++ b/radiantcore/decl/DeclarationManager.cpp @@ -256,28 +256,26 @@ void DeclarationManager::waitForTypedParsersToFinish() // Acquire the lock to modify the cleanup tasks list auto declLock = std::make_unique>(_declarationAndCreatorLock); - // Add the task to the list, we need to wait for it when - // shutting down the module - auto& task = _parserCleanupTasks.emplace_back( - std::async(std::launch::async, [this]() - { - // Extract all parsers while we hold the lock - std::vector> parsersToFinish; + // Extract all parsers while we hold the lock + std::vector> parsersToFinish; - { - std::lock_guard declLock(_declarationAndCreatorLock); + for (auto& [_, decl] : _declarationsByType) + { + if (decl.parser) + { + parsersToFinish.emplace_back(std::move(decl.parser)); + } + } - for (auto& [_, decl] : _declarationsByType) - { - if (decl.parser) - { - parsersToFinish.emplace_back(std::move(decl.parser)); - } - } - } + if (parsersToFinish.empty()) return; // nothing to do - // With the lock released, let all parsers finish their work - parsersToFinish.clear(); + // Add the task to the list, we need to wait for it when shutting down the module + // Move the collected parsers to the lambda + auto& task = _parserCleanupTasks.emplace_back( + std::async(std::launch::async, [parsers = std::move(parsersToFinish)]() mutable + { + // Without locking anything, just let all parsers finish their work + parsers.clear(); }) );