From 974a6e30273c675dd267d632976977c1b1362a3b Mon Sep 17 00:00:00 2001 From: Antonio Filipovic <61245998+antoniofilipovic@users.noreply.github.com> Date: Thu, 7 Sep 2023 17:42:27 +0200 Subject: [PATCH] Fix bug on mgp dispatcher guard (#1225) --- include/mgp.hpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/include/mgp.hpp b/include/mgp.hpp index 2b1f0318d..8ffeeee46 100644 --- a/include/mgp.hpp +++ b/include/mgp.hpp @@ -126,6 +126,12 @@ class MemoryDispatcher final { map_.erase(this_id); } + bool IsThisThreadRegistered() noexcept { + const auto this_id = std::this_thread::get_id(); + std::shared_lock lock(mut_); + return map_.contains(this_id); + } + private: std::unordered_map map_; std::shared_mutex mut_; @@ -136,7 +142,7 @@ class MemoryDispatcher final { // header. The use of the 'mgp_memory *memory' pointer is deprecated // and will be removed in upcoming releases. // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) -inline extern MemoryDispatcher mrd{}; +inline MemoryDispatcher mrd{}; // TODO - Once we deprecate this we should remove this // and make sure nothing relies on it anymore. This alone @@ -164,7 +170,7 @@ class MemoryDispatcherGuard final { // the mapping instead. template inline decltype(auto) MemHandlerCallback(Func &&func, Args &&...args) { - if (memory) { + if (!mrd.IsThisThreadRegistered()) { return std::forward(func)(std::forward(args)..., memory); } return std::forward(func)(std::forward(args)..., mrd.GetMemoryResource());