Fixed memory leaks

Summary:
MyRocks valgrind run was showing memory leaks. The fixes are mostly self-explaining.
There is only a single usage of ThreadLocalPtr. Potentially, we may think about replacing this use with thread_local, but it will be a bigger change. Another option to consider is using thread_local instead of __thread in ThreadLocalPtr implementation. This way, tls_ can be stored using std::unique_ptr and no destructor would be required.

Test Plan:
 - make check
 - MyRocks valgrind run doesn't report leaks

Reviewers: rven, sdong

Reviewed By: sdong

Subscribers: dhruba

Differential Revision: https://reviews.facebook.net/D43677
main
Alexey Maykov 9 years ago
parent 254c4fb88f
commit 257ee895f9
  1. 13
      util/comparator.cc
  2. 11
      util/thread_local.cc

@ -8,6 +8,7 @@
// found in the LICENSE file. See the AUTHORS file for names of contributors.
#include <algorithm>
#include <memory>
#include <stdint.h>
#include "rocksdb/comparator.h"
#include "rocksdb/slice.h"
@ -85,22 +86,22 @@ class ReverseBytewiseComparatorImpl : public BytewiseComparatorImpl {
}// namespace
static port::OnceType once = LEVELDB_ONCE_INIT;
static const Comparator* bytewise;
static const Comparator* rbytewise;
static std::unique_ptr<const Comparator> bytewise;
static std::unique_ptr<const Comparator> rbytewise;
static void InitModule() {
bytewise = new BytewiseComparatorImpl;
rbytewise= new ReverseBytewiseComparatorImpl;
bytewise.reset(new BytewiseComparatorImpl);
rbytewise.reset(new ReverseBytewiseComparatorImpl);
}
const Comparator* BytewiseComparator() {
port::InitOnce(&once, InitModule);
return bytewise;
return bytewise.get();
}
const Comparator* ReverseBytewiseComparator() {
port::InitOnce(&once, InitModule);
return rbytewise;
return rbytewise.get();
}
} // namespace rocksdb

@ -137,6 +137,17 @@ ThreadLocalPtr::StaticMeta::StaticMeta() : next_instance_id_(0) {
if (pthread_key_create(&pthread_key_, &OnThreadExit) != 0) {
abort();
}
// OnThreadExit is not getting called on the main thread.
// Call through the static destructor mechanism to avoid memory leak.
static struct A {
~A() {
if (tls_) {
OnThreadExit(tls_);
}
}
} a;
head_.next = &head_;
head_.prev = &head_;

Loading…
Cancel
Save