From b1caece80daeab2e05ac2e5c6b3550676022ed94 Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Thu, 13 Jul 2017 16:38:42 +0300 Subject: [core] Make sure ThreadLocal will not own the pointer it is managing ThreadLocal should not own the pointer it is managing because the use case in Mapbox GL is to keep a pointer to a stack allocated object, like: ``` MyObject foo; threadLocal.set(&foo); ``` To keep consistency, it is required that we clear the managed object before ThreadLocal gets destroyed by setting it to `nullptr`. --- platform/default/thread_local.cpp | 6 +++++- platform/qt/src/thread_local.cpp | 7 ++++++- scripts/valgrind.sup | 8 -------- test/util/thread_local.test.cpp | 19 +++++++++++-------- 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/platform/default/thread_local.cpp b/platform/default/thread_local.cpp index b754a04b7d..6fdb1e6dc1 100644 --- a/platform/default/thread_local.cpp +++ b/platform/default/thread_local.cpp @@ -29,7 +29,11 @@ ThreadLocal::ThreadLocal() : impl(std::make_unique()) { template ThreadLocal::~ThreadLocal() { - delete get(); + // ThreadLocal will not take ownership + // of the pointer it is managing. The pointer + // needs to be explicitly cleared before we + // destroy this object. + assert(!get()); if (pthread_key_delete(impl->key)) { Log::Error(Event::General, "Failed to delete local storage key."); diff --git a/platform/qt/src/thread_local.cpp b/platform/qt/src/thread_local.cpp index e48a9d6e74..e835a680e2 100644 --- a/platform/qt/src/thread_local.cpp +++ b/platform/qt/src/thread_local.cpp @@ -4,6 +4,7 @@ #include #include +#include #include @@ -23,7 +24,11 @@ ThreadLocal::ThreadLocal() : impl(std::make_unique()) { template ThreadLocal::~ThreadLocal() { - delete get(); + // ThreadLocal will not take ownership + // of the pointer it is managing. The pointer + // needs to be explicitly cleared before we + // destroy this object. + assert(!get()); } template diff --git a/scripts/valgrind.sup b/scripts/valgrind.sup index f659122dc1..09e2f6685c 100644 --- a/scripts/valgrind.sup +++ b/scripts/valgrind.sup @@ -76,11 +76,3 @@ fun:_ZN4mbgl4util10write_fileERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES8_ ... } -{ - Qt5 ThreadStorage - Memcheck:Leak - match-leak-kinds: definite - fun:_Znwm - fun:_Z24qThreadStorage_localDataISt5arrayIPiLm1EEERT_R18QThreadStorageDataPS3_.isra.5 - ... -} diff --git a/test/util/thread_local.test.cpp b/test/util/thread_local.test.cpp index 12a19ab59b..8016d49052 100644 --- a/test/util/thread_local.test.cpp +++ b/test/util/thread_local.test.cpp @@ -77,24 +77,27 @@ public: data.set(data_); } + ~TestThreadReclaim() { + data.set(nullptr); + } + private: ThreadLocal data; }; } // namespace -TEST(ThreadLocalStorage, AutoReclaim) { +TEST(ThreadLocalStorage, ShouldNotTakeOwnership) { RunLoop loop; - auto data1 = new int; - auto data2 = new int; + auto data1 = std::make_unique(10); + auto data2 = std::make_unique(20); - auto thread1 = std::make_unique>("Test", data1); - auto thread2 = std::make_unique>("Test", data2); + auto thread1 = std::make_unique>("Test", data1.get()); + auto thread2 = std::make_unique>("Test", data2.get()); + // Will crash if ThreadLocal destroys + // the pointer it is managing. thread1.reset(); thread2.reset(); - - // Should not leak, valgrind will - // let us know. } -- cgit v1.2.1