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

Maksym Wezdecki via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 28 02:09:41 PDT 2020


mwezdeck updated this revision to Diff 281147.
mwezdeck added a comment.

Second initialization is required to handle corner cases. It will not happen in real world application. There is call for ManagedStatic's function after llvm_shutdown in DynamicLibrary unit-test, this second initialization is for covering that.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D83372/new/

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
@@ -27,6 +27,13 @@
 
 static std::recursive_mutex *getManagedStaticMutex() {
   llvm::call_once(mutex_init_flag, initializeMutex);
+  // corner case handling - for tests purposes
+  // [this 'if' will not be triggered in real world app]
+  // it is for covering test case where RegisterManagedStatic
+  // is called AFTER llvm_shutdown
+  if (ManagedStaticMutex == nullptr) {
+    initializeMutex();
+  }
   return ManagedStaticMutex;
 }
 
@@ -76,8 +83,12 @@
 
 /// 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(*getManagedStaticMutex());
 
-  while (StaticList)
-    StaticList->destroy();
+    while (StaticList)
+      StaticList->destroy();
+  }
+  delete ManagedStaticMutex;
+  ManagedStaticMutex = nullptr;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D83372.281147.patch
Type: text/x-patch
Size: 1082 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200728/167954de/attachment.bin>


More information about the llvm-commits mailing list