aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Support/Timer.cpp
diff options
context:
space:
mode:
authorAlexandre Ganea <aganea@havenstudios.com>2025-09-22 08:41:22 -0400
committerGitHub <noreply@github.com>2025-09-22 08:41:22 -0400
commite52792e2f830df743095d5e1bc15b4bfb51d40f3 (patch)
treeef81341483d802096fb0992a7d58e83b1990e432 /llvm/lib/Support/Timer.cpp
parentda55134db3c82d1169df73f91de2f18b42016045 (diff)
downloadllvm-e52792e2f830df743095d5e1bc15b4bfb51d40f3.zip
llvm-e52792e2f830df743095d5e1bc15b4bfb51d40f3.tar.gz
llvm-e52792e2f830df743095d5e1bc15b4bfb51d40f3.tar.bz2
[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" />
Diffstat (limited to 'llvm/lib/Support/Timer.cpp')
-rw-r--r--llvm/lib/Support/Timer.cpp25
1 files changed, 21 insertions, 4 deletions
diff --git a/llvm/lib/Support/Timer.cpp b/llvm/lib/Support/Timer.cpp
index 75ec299..67483ba 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();
+}