summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorThiago Marcos P. Santos <tmpsantos@gmail.com>2017-07-13 16:38:42 +0300
committerThiago Marcos P. Santos <tmpsantos@gmail.com>2017-07-14 15:18:54 +0300
commit48ddfef89ff4daa2e26d3338dbfc20c4f4cc7d21 (patch)
treedfe3e3518719411f97dc441fb14f4ddfc5af47fd /test
parent3b26177e9d3d9ed87f96ac33e7ed74bc7653f661 (diff)
downloadqtlocation-mapboxgl-48ddfef89ff4daa2e26d3338dbfc20c4f4cc7d21.tar.gz
[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`.
Diffstat (limited to 'test')
-rw-r--r--test/util/thread_local.test.cpp28
1 files changed, 18 insertions, 10 deletions
diff --git a/test/util/thread_local.test.cpp b/test/util/thread_local.test.cpp
index 12a19ab59b..7142697f48 100644
--- a/test/util/thread_local.test.cpp
+++ b/test/util/thread_local.test.cpp
@@ -71,30 +71,38 @@ TEST(ThreadLocalStorage, NotSetReturnsNull) {
namespace {
-class TestThreadReclaim {
+class TestThreadDataOwnership {
public:
- TestThreadReclaim(mbgl::ActorRef<TestThreadReclaim>, int* data_) {
+ TestThreadDataOwnership(mbgl::ActorRef<TestThreadDataOwnership>, int* data_) {
data.set(data_);
}
+ ~TestThreadDataOwnership() {
+ data.set(nullptr);
+ }
+
private:
- ThreadLocal<int> data;
+ static ThreadLocal<int> data;
};
+ThreadLocal<int> TestThreadDataOwnership::data;
+
} // namespace
-TEST(ThreadLocalStorage, AutoReclaim) {
+TEST(ThreadLocalStorage, ShouldNotTakeOwnership) {
RunLoop loop;
- auto data1 = new int;
- auto data2 = new int;
+ auto data1 = std::make_unique<int>(10);
+ auto data2 = std::make_unique<int>(20);
- auto thread1 = std::make_unique<Thread<TestThreadReclaim>>("Test", data1);
- auto thread2 = std::make_unique<Thread<TestThreadReclaim>>("Test", data2);
+ auto thread1 = std::make_unique<Thread<TestThreadDataOwnership>>("Test", data1.get());
+ auto thread2 = std::make_unique<Thread<TestThreadDataOwnership>>("Test", data2.get());
thread1.reset();
thread2.reset();
- // Should not leak, valgrind will
- // let us know.
+ // Will crash if ThreadLocal destroys
+ // the pointer it is managing.
+ ASSERT_EQ(*data1, 10);
+ ASSERT_EQ(*data2, 20);
}