[llvm] 56349e8 - Fix for memory leak reported by Valgrind

David Blaikie via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 16 11:20:13 PDT 2021


Author: Maksym Wezdecki
Date: 2021-03-16T11:01:31-07:00
New Revision: 56349e8b6d85621d4d95efe27716b3f6974d4324

URL: https://github.com/llvm/llvm-project/commit/56349e8b6d85621d4d95efe27716b3f6974d4324
DIFF: https://github.com/llvm/llvm-project/commit/56349e8b6d85621d4d95efe27716b3f6974d4324.diff

LOG: Fix for memory leak reported by Valgrind

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

This patch fixes the issue.

Reviewed By: lattner, dblaikie

Differential Revision: https://reviews.llvm.org/D83372

Added: 
    

Modified: 
    llvm/lib/Support/ManagedStatic.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Support/ManagedStatic.cpp b/llvm/lib/Support/ManagedStatic.cpp
index 053493f72fb5..a6ae67066ea0 100644
--- a/llvm/lib/Support/ManagedStatic.cpp
+++ b/llvm/lib/Support/ManagedStatic.cpp
@@ -18,16 +18,10 @@
 using namespace llvm;
 
 static const ManagedStaticBase *StaticList = nullptr;
-static std::recursive_mutex *ManagedStaticMutex = nullptr;
-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;
+  static std::recursive_mutex m;
+  return &m;
 }
 
 void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
@@ -75,9 +69,10 @@ void ManagedStaticBase::destroy() const {
 }
 
 /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
+/// IMPORTANT: it's only safe to call llvm_shutdown() in single thread,
+/// without any other threads executing LLVM APIs.
+/// llvm_shutdown() should be the last use of LLVM APIs.
 void llvm::llvm_shutdown() {
-  std::lock_guard<std::recursive_mutex> Lock(*getManagedStaticMutex());
-
   while (StaticList)
     StaticList->destroy();
 }


        


More information about the llvm-commits mailing list