Skip to content

Commit 5d7d09a

Browse files
committed
[Support] Fix memory leak in Timer on shutdown
1 parent 105fc90 commit 5d7d09a

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

llvm/lib/Support/Timer.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ static SignpostEmitter &signposts();
5757
static sys::SmartMutex<true> &timerLock();
5858
static TimerGroup &defaultTimerGroup();
5959
static Name2PairMap &namedGroupedTimers();
60+
static bool isTimerGlobalsConstructed();
6061

6162
//===----------------------------------------------------------------------===//
6263
//
@@ -305,14 +306,24 @@ TimerGroup::~TimerGroup() {
305306
PrintQueuedTimers(*OutStream);
306307
}
307308

309+
auto unlink = [&]() {
310+
*Prev = Next;
311+
if (Next)
312+
Next->Prev = Prev;
313+
};
314+
315+
// If the managed instance is already dead, it means we're in the CRT
316+
// destruction, so no need to lock.
317+
if (!isTimerGlobalsConstructed()) {
318+
unlink();
319+
return;
320+
}
321+
308322
// Remove the group from the TimerGroupList.
309323
sys::SmartScopedLock<true> L(timerLock());
310-
*Prev = Next;
311-
if (Next)
312-
Next->Prev = Prev;
324+
unlink();
313325
}
314326

315-
316327
void TimerGroup::removeTimer(Timer &T) {
317328
sys::SmartScopedLock<true> L(timerLock());
318329

@@ -557,3 +568,7 @@ void TimerGroup::constructForStatistics() {
557568
}
558569

559570
void *TimerGroup::acquireTimerGlobals() { return ManagedTimerGlobals.claim(); }
571+
572+
static bool isTimerGlobalsConstructed() {
573+
return ManagedTimerGlobals.isConstructed();
574+
}

0 commit comments

Comments
 (0)