From d87c79ac1487fbdc220b9ddd419ca2e6165b13ad Mon Sep 17 00:00:00 2001 From: Trish Lam Date: Mon, 6 Feb 2023 12:01:53 -0800 Subject: [PATCH] Solving leaks within Windows --- .../phonenumbers/base/memory/singleton_win32.h | 16 +++++++--------- cpp/src/phonenumbers/phonenumberutil.cc | 6 ++++++ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/cpp/src/phonenumbers/base/memory/singleton_win32.h b/cpp/src/phonenumbers/base/memory/singleton_win32.h index 7da69e8a1..0cfc05bed 100644 --- a/cpp/src/phonenumbers/base/memory/singleton_win32.h +++ b/cpp/src/phonenumbers/base/memory/singleton_win32.h @@ -30,14 +30,12 @@ class Singleton { virtual ~Singleton() {} static T* GetInstance() { + AcquireSRWLockExclusive(&singleton_lock_); if (once_init_) { - EnterCriticalSection(&critical_section_); - if (once_init_) { - Init(); - once_init_ = false; - } - LeaveCriticalSection(&critical_section_); + Init(); + once_init_ = false; } + ReleaseSRWLockExclusive(&singleton_lock_); return instance_; } @@ -49,7 +47,7 @@ class Singleton { } static T* instance_; // Leaky singleton. - static CRITICAL_SECTION critical_section_; + static SRWLOCK singleton_lock_; static bool once_init_; }; @@ -60,8 +58,8 @@ static bool perform_init_crit(CRITICAL_SECTION& cs) } template T* Singleton::instance_; -template CRITICAL_SECTION Singleton::critical_section_; -template bool Singleton::once_init_=perform_init_crit(Singleton::critical_section_); +template SRWLOCK Singleton::singleton_lock_ = SRWLOCK_INIT; +template bool Singleton::once_init_ = true; } // namespace phonenumbers } // namespace i18n diff --git a/cpp/src/phonenumbers/phonenumberutil.cc b/cpp/src/phonenumbers/phonenumberutil.cc index 5ce540e47..ae26bd2a1 100644 --- a/cpp/src/phonenumbers/phonenumberutil.cc +++ b/cpp/src/phonenumbers/phonenumberutil.cc @@ -888,6 +888,12 @@ PhoneNumberUtil::~PhoneNumberUtil() { gtl::STLDeleteContainerPairSecondPointers( country_calling_code_to_region_code_map_->begin(), country_calling_code_to_region_code_map_->end()); + + for (std::map::iterator it = region_to_metadata_map_->begin(); + it != region_to_metadata_map_->end(); + ++it) { + it->second.Clear(); + } } void PhoneNumberUtil::GetSupportedRegions(std::set* regions)