[llvm] [Support] Fix memory leak in `Timer.cpp` on shutdown (PR #159983)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Sep 21 08:11:55 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-support
Author: Alexandre Ganea (aganea)
<details>
<summary>Changes</summary>
This used to happen in the global destruction, after `main()` has exited. Previously, we were re-creating the `llvm::TimerGlobals` object at this point.
<img width="855" height="270" alt="image" src="https://github.com/user-attachments/assets/757e9416-a74a-406a-841e-d3e4cc6a69a1" />
---
Full diff: https://github.com/llvm/llvm-project/pull/159983.diff
1 Files Affected:
- (modified) llvm/lib/Support/Timer.cpp (+19-4)
``````````diff
diff --git a/llvm/lib/Support/Timer.cpp b/llvm/lib/Support/Timer.cpp
index 75ec299a98376..20f51afe4ee74 100644
--- a/llvm/lib/Support/Timer.cpp
+++ b/llvm/lib/Support/Timer.cpp
@@ -57,6 +57,7 @@ static SignpostEmitter &signposts();
static sys::SmartMutex<true> &timerLock();
static TimerGroup &defaultTimerGroup();
static Name2PairMap &namedGroupedTimers();
+static bool isTimerGlobalsConstructed();
//===----------------------------------------------------------------------===//
//
@@ -305,14 +306,24 @@ TimerGroup::~TimerGroup() {
PrintQueuedTimers(*OutStream);
}
+ auto unlink = [&]() {
+ *Prev = Next;
+ if (Next)
+ Next->Prev = Prev;
+ };
+
+ // If the managed instance is already dead, it means we're in the CRT
+ // destruction, so no need to lock.
+ if (!isTimerGlobalsConstructed()) {
+ unlink();
+ return;
+ }
+
// Remove the group from the TimerGroupList.
sys::SmartScopedLock<true> L(timerLock());
- *Prev = Next;
- if (Next)
- Next->Prev = Prev;
+ unlink();
}
-
void TimerGroup::removeTimer(Timer &T) {
sys::SmartScopedLock<true> L(timerLock());
@@ -557,3 +568,7 @@ void TimerGroup::constructForStatistics() {
}
void *TimerGroup::acquireTimerGlobals() { return ManagedTimerGlobals.claim(); }
+
+static bool isTimerGlobalsConstructed() {
+ return ManagedTimerGlobals.isConstructed();
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/159983
More information about the llvm-commits
mailing list