[PATCH] D83372: Fix for memory leak reported by Valgrind

Maksym Wezdecki via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 8 00:54:10 PDT 2020


mwezdeck created this revision.
mwezdeck added reviewers: lattner, dblaikie, resistor, bkramer, craig.topper.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

If llvm so lib is dlopened and dlclosed several times, then memory leak can be observed, reported by Valgrind.

This patch fixes the issue.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D83372

Files:
  llvm/lib/Support/ManagedStatic.cpp


Index: llvm/lib/Support/ManagedStatic.cpp
===================================================================
--- llvm/lib/Support/ManagedStatic.cpp
+++ llvm/lib/Support/ManagedStatic.cpp
@@ -18,23 +18,14 @@
 using namespace llvm;
 
 static const ManagedStaticBase *StaticList = nullptr;
-static std::recursive_mutex *ManagedStaticMutex = nullptr;
+static std::recursive_mutex ManagedStaticMutex;
 static llvm::once_flag mutex_init_flag;
 
-static void initializeMutex() {
-  ManagedStaticMutex = new std::recursive_mutex();
-}
-
-static std::recursive_mutex *getManagedStaticMutex() {
-  llvm::call_once(mutex_init_flag, initializeMutex);
-  return ManagedStaticMutex;
-}
-
 void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
                                               void (*Deleter)(void*)) const {
   assert(Creator);
   if (llvm_is_multithreaded()) {
-    std::lock_guard<std::recursive_mutex> Lock(*getManagedStaticMutex());
+    std::lock_guard<std::recursive_mutex> Lock(ManagedStaticMutex);
 
     if (!Ptr.load(std::memory_order_relaxed)) {
       void *Tmp = Creator();
@@ -76,7 +67,7 @@
 
 /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
 void llvm::llvm_shutdown() {
-  std::lock_guard<std::recursive_mutex> Lock(*getManagedStaticMutex());
+  std::lock_guard<std::recursive_mutex> Lock(ManagedStaticMutex);
 
   while (StaticList)
     StaticList->destroy();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D83372.276326.patch
Type: text/x-patch
Size: 1415 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200708/ef4f394b/attachment.bin>


More information about the llvm-commits mailing list