From 18cc5018b7454d931cff623302c5e7216f112f99 Mon Sep 17 00:00:00 2001 From: Aaron Feldman Date: Fri, 19 Jun 2015 09:43:08 -0700 Subject: [PATCH] Fix memory leaks in PinnedUsageTest Summary: See title Test Plan: Run valgrind ./cache_test Reviewers: igor Reviewed By: igor Subscribers: anthony, dhruba Differential Revision: https://reviews.facebook.net/D40419 --- util/cache_test.cc | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/util/cache_test.cc b/util/cache_test.cc index 86e326b53..c8b2de8f5 100644 --- a/util/cache_test.cc +++ b/util/cache_test.cc @@ -9,6 +9,7 @@ #include "rocksdb/cache.h" +#include #include #include #include @@ -150,6 +151,8 @@ TEST_F(CacheTest, PinnedUsageTest) { size_t pinned_usage = 0; const char* value = "abcdef"; + std::forward_list unreleased_handles; + // Add entries. Unpin some of them after insertion. Then, pin some of them // again. Check GetPinnedUsage(). for (int i = 1; i < 100; ++i) { @@ -162,9 +165,11 @@ TEST_F(CacheTest, PinnedUsageTest) { cache->Release(handle); pinned_usage -= kv_size; ASSERT_EQ(pinned_usage, cache->GetPinnedUsage()); + } else { + unreleased_handles.push_front(handle); } if (i % 3 == 0) { - cache->Lookup(key); + unreleased_handles.push_front(cache->Lookup(key)); // If i % 2 == 0, then the entry was unpinned before Lookup, so pinned // usage increased if (i % 2 == 0) { @@ -181,6 +186,11 @@ TEST_F(CacheTest, PinnedUsageTest) { cache->Insert(key, (void*)value, key.size() + 5, dumbDeleter)); } ASSERT_EQ(pinned_usage, cache->GetPinnedUsage()); + + // release handles for pinned entries to prevent memory leaks + for (auto handle : unreleased_handles) { + cache->Release(handle); + } } TEST_F(CacheTest, HitAndMiss) {