[llvm] e52792e - [Support] Fix memory leak in `Timer.cpp` on shutdown (#159983)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 22 05:41:26 PDT 2025
Author: Alexandre Ganea
Date: 2025-09-22T08:41:22-04:00
New Revision: e52792e2f830df743095d5e1bc15b4bfb51d40f3
URL: https://github.com/llvm/llvm-project/commit/e52792e2f830df743095d5e1bc15b4bfb51d40f3
DIFF: https://github.com/llvm/llvm-project/commit/e52792e2f830df743095d5e1bc15b4bfb51d40f3.diff
LOG: [Support] Fix memory leak in `Timer.cpp` on shutdown (#159983)
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"
/>
Added:
Modified:
llvm/lib/Support/Timer.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Support/Timer.cpp b/llvm/lib/Support/Timer.cpp
index 75ec299a98376..67483ba9dd655 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,26 @@ TimerGroup::~TimerGroup() {
PrintQueuedTimers(*OutStream);
}
+ auto unlink = [&]() {
+ *Prev = Next;
+ if (Next)
+ Next->Prev = Prev;
+ };
+
+ // TimerGlobals is always created implicity, through a call to timerLock(),
+ // when a TimeGroup is created. On CRT shutdown, the TimerGlobals instance
+ // might have been destroyed already. Avoid re-creating it if calling
+ // timerLock().
+ 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 +570,7 @@ void TimerGroup::constructForStatistics() {
}
void *TimerGroup::acquireTimerGlobals() { return ManagedTimerGlobals.claim(); }
+
+static bool isTimerGlobalsConstructed() {
+ return ManagedTimerGlobals.isConstructed();
+}
More information about the llvm-commits
mailing list