Use NoDestructor in a couple obvious places. Use NoDestructor for some deliberately leaked timer maps. Fix some nits to slightly simplify the code, and remove time vs. timer naming confusion. Change-Id: I57db73e50891dc58f5bae2666a3618699d3a76bf Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/64450 Reviewed-by: Tom Sepez <tsepez@chromium.org> Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxcrt/cfx_timer.cpp b/core/fxcrt/cfx_timer.cpp index 057cda6..6ffb625 100644 --- a/core/fxcrt/cfx_timer.cpp +++ b/core/fxcrt/cfx_timer.cpp
@@ -8,12 +8,14 @@ #include <map> +#include "third_party/base/no_destructor.h" + namespace { -std::map<int32_t, CFX_Timer*>& GetPWLTimeMap() { - // Leak the object at shutdown. - static auto* timeMap = new std::map<int32_t, CFX_Timer*>; - return *timeMap; +using TimerMap = std::map<int32_t, CFX_Timer*>; +TimerMap& GetPWLTimerMap() { + static pdfium::base::NoDestructor<TimerMap> timer_map; + return *timer_map; } } // namespace @@ -26,19 +28,19 @@ m_pCallbackIface(pCallbackIface) { ASSERT(m_pCallbackIface); if (HasValidID()) - GetPWLTimeMap()[m_nTimerID] = this; + GetPWLTimerMap()[m_nTimerID] = this; } CFX_Timer::~CFX_Timer() { if (HasValidID()) { m_pTimerHandler->KillTimer(m_nTimerID); - GetPWLTimeMap().erase(m_nTimerID); + GetPWLTimerMap().erase(m_nTimerID); } } // static void CFX_Timer::TimerProc(int32_t idEvent) { - auto it = GetPWLTimeMap().find(idEvent); - if (it != GetPWLTimeMap().end()) + auto it = GetPWLTimerMap().find(idEvent); + if (it != GetPWLTimerMap().end()) it->second->m_pCallbackIface->OnTimerFired(); }
diff --git a/fxjs/global_timer.cpp b/fxjs/global_timer.cpp index b3049a0..b837508 100644 --- a/fxjs/global_timer.cpp +++ b/fxjs/global_timer.cpp
@@ -10,14 +10,14 @@ #include "core/fxcrt/timerhandler_iface.h" #include "fxjs/cjs_app.h" +#include "third_party/base/no_destructor.h" namespace { using TimerMap = std::map<int32_t, GlobalTimer*>; -TimerMap* GetGlobalTimerMap() { - // Leak the timer array at shutdown. - static auto* s_TimerMap = new TimerMap; - return s_TimerMap; +TimerMap& GetGlobalTimerMap() { + static pdfium::base::NoDestructor<TimerMap> timer_map; + return *timer_map; } } // namespace @@ -35,7 +35,7 @@ m_pRuntime(pRuntime), m_pEmbedApp(pObj) { if (HasValidID()) - (*GetGlobalTimerMap())[m_nTimerID] = this; + GetGlobalTimerMap()[m_nTimerID] = this; } GlobalTimer::~GlobalTimer() { @@ -45,13 +45,13 @@ if (m_pRuntime && m_pRuntime->GetTimerHandler()) m_pRuntime->GetTimerHandler()->KillTimer(m_nTimerID); - GetGlobalTimerMap()->erase(m_nTimerID); + GetGlobalTimerMap().erase(m_nTimerID); } // static void GlobalTimer::Trigger(int32_t nTimerID) { - auto it = GetGlobalTimerMap()->find(nTimerID); - if (it == GetGlobalTimerMap()->end()) + auto it = GetGlobalTimerMap().find(nTimerID); + if (it == GetGlobalTimerMap().end()) return; GlobalTimer* pTimer = it->second; @@ -63,8 +63,8 @@ pTimer->m_pEmbedApp->TimerProc(pTimer); // Timer proc may have destroyed timer, find it again. - it = GetGlobalTimerMap()->find(nTimerID); - if (it == GetGlobalTimerMap()->end()) + it = GetGlobalTimerMap().find(nTimerID); + if (it == GetGlobalTimerMap().end()) return; pTimer = it->second; @@ -75,8 +75,8 @@ // static void GlobalTimer::Cancel(int32_t nTimerID) { - auto it = GetGlobalTimerMap()->find(nTimerID); - if (it == GetGlobalTimerMap()->end()) + auto it = GetGlobalTimerMap().find(nTimerID); + if (it == GetGlobalTimerMap().end()) return; GlobalTimer* pTimer = it->second;