From c6c76154d1e18d3048f22ccc4888b3e2fb07fe89 Mon Sep 17 00:00:00 2001 From: "Paul J. Davis" Date: Sun, 18 Feb 2024 05:39:14 -0600 Subject: [PATCH] Move to a process global for test MemoryTracker (#4739) I've seen about three or four different test failures due to `create_test_memory_tracker` returning new instances which leads to life time issues with the memory resources. This changes to a single process singleton to avoid the issue altogether. Once we merge the current PRs that are all using `create_test_memory_tracker` I'll follow up with a PR to rename all of those instances to `get_test_memory_tracker` and remove the `create_test_memory_tracker` to prevent further use. --- TYPE: NO_HISTORY DESC: Move to a process global for test MemoryTracker --- test/support/src/mem_helpers.cc | 14 ++++++++++++-- test/support/src/mem_helpers.h | 12 ++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/test/support/src/mem_helpers.cc b/test/support/src/mem_helpers.cc index c92f0cab85f..50a18ad5b26 100644 --- a/test/support/src/mem_helpers.cc +++ b/test/support/src/mem_helpers.cc @@ -35,15 +35,25 @@ namespace tiledb::test { -shared_ptr create_test_memory_tracker() { +shared_ptr get_test_memory_tracker() { class MemoryTrackerCreator : public sm::MemoryTracker { public: MemoryTrackerCreator() : sm::MemoryTracker() { } + + static shared_ptr get_instance() { + static shared_ptr tracker{ + new MemoryTrackerCreator()}; + return tracker; + } }; - return make_shared(HERE()); + return MemoryTrackerCreator::get_instance(); +} + +shared_ptr create_test_memory_tracker() { + return get_test_memory_tracker(); } } // namespace tiledb::test diff --git a/test/support/src/mem_helpers.h b/test/support/src/mem_helpers.h index 96a359a1975..363a1e78a76 100644 --- a/test/support/src/mem_helpers.h +++ b/test/support/src/mem_helpers.h @@ -38,6 +38,18 @@ namespace tiledb::test { +/** + * Helper function get the test instance of a shared_ptr + * + * This is the preferred function. The create_test_memory_tracker will be + * replaced shortly and only serves as a proxy to this function while we + * transition the first few PRs to use this new function. + * + * The reasoning here is that creating memory trackers has turned out to be a + * bit of a footgun with lifetime issues. + */ +shared_ptr get_test_memory_tracker(); + /** * Helper function to create test instances of shared_ptr */