From 5101e82e35806c1bc9de9ba78a770f8cfabbdcae Mon Sep 17 00:00:00 2001 From: Bryan Call Date: Tue, 11 Oct 2016 12:58:01 -0700 Subject: [PATCH] TS-4956: Memory leaks in hostdb test --- iocore/hostdb/P_RefCountCache.h | 5 +++- iocore/hostdb/test_RefCountCache.cc | 36 ++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/iocore/hostdb/P_RefCountCache.h b/iocore/hostdb/P_RefCountCache.h index 5f69a2aa75b..d3b45ad9ec1 100644 --- a/iocore/hostdb/P_RefCountCache.h +++ b/iocore/hostdb/P_RefCountCache.h @@ -445,7 +445,10 @@ RefCountCache::RefCountCache(unsigned int num_partitions, int size, int items // Deconstruct the class template RefCountCache::~RefCountCache() { - delete this->partitions; + for (unsigned int i = 0; i < num_partitions; i++) { + delete this->partitions[i]; + } + num_partitions = 0; } template diff --git a/iocore/hostdb/test_RefCountCache.cc b/iocore/hostdb/test_RefCountCache.cc index 05e50aef9ea..c6a3a80233d 100644 --- a/iocore/hostdb/test_RefCountCache.cc +++ b/iocore/hostdb/test_RefCountCache.cc @@ -26,6 +26,7 @@ #include #include #include +#include // TODO: add tests with expiry_time @@ -34,6 +35,7 @@ class ExampleStruct : public RefCountObj public: int idx; int name_offset; // pointer addr to name + static std::set items_freed; // Return the char* to the name (TODO: cleaner interface??) char * @@ -48,11 +50,20 @@ class ExampleStruct : public RefCountObj return new (malloc(sizeof(ExampleStruct) + size)) ExampleStruct(); } - // To mark it as "deleted" (so its easy to check) we'll just mark the idx as -1 + static void + dealloc(ExampleStruct *e) + { + e->~ExampleStruct(); + ::free(e); + } + + // Really free the memory, we can use asan leak detection to verify it was freed void free() { this->idx = -1; + items_freed.insert(this); + printf("freeing: %p items_freed.size(): %zd\n", this, items_freed.size()); } static ExampleStruct * @@ -70,6 +81,8 @@ class ExampleStruct : public RefCountObj } }; +std::set ExampleStruct::items_freed; + void fillCache(RefCountCache *cache, int start, int end) { @@ -166,6 +179,8 @@ testRefcounting() ret |= tmpAfter.get()->idx != 1; printf("ret=%d ref=%d\n", ret, tmp->refcount()); + delete cache; + return ret; } @@ -189,7 +204,7 @@ testclear() } int -main() +test() { // Initialize IOBufAllocator RecModeT mode_type = RECM_STAND_ALONE; @@ -264,7 +279,22 @@ main() // printf("Sync return: %d\n", cache->sync_all()); printf("TestRun: %d\n", ret); - exit(ret); + + delete cache; + + return ret; +} + +int +main() +{ + int ret = test(); + + for (const auto item : ExampleStruct::items_freed) { + printf("really freeing: %p\n", item); + ExampleStruct::dealloc(item); + } + ExampleStruct::items_freed.clear(); return ret; }