From 545bf260e19df91d311e35adb4e456f78d52c016 Mon Sep 17 00:00:00 2001 From: Frank Tang Date: Mon, 17 Mar 2025 21:46:55 -0700 Subject: [PATCH] ICU-22817 Fix potential memory leak by using LocalPointer See https://github.com/LuMingYinDetect/icu_defects/blob/main/icu_10.png --- icu4c/source/i18n/tzgnames.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/icu4c/source/i18n/tzgnames.cpp b/icu4c/source/i18n/tzgnames.cpp index e7b09efd01a..965038ac152 100644 --- a/icu4c/source/i18n/tzgnames.cpp +++ b/icu4c/source/i18n/tzgnames.cpp @@ -1172,9 +1172,8 @@ TimeZoneGenericNames::createInstance(const Locale& locale, UErrorCode& status) { if (U_FAILURE(status)) { return nullptr; } - TimeZoneGenericNames* instance = new TimeZoneGenericNames(); - if (instance == nullptr) { - status = U_MEMORY_ALLOCATION_ERROR; + LocalPointer instance(new TimeZoneGenericNames(), status); + if (U_FAILURE(status)) { return nullptr; } @@ -1193,7 +1192,6 @@ TimeZoneGenericNames::createInstance(const Locale& locale, UErrorCode& status) { } } if (U_FAILURE(status)) { - delete instance; return nullptr; } @@ -1252,12 +1250,11 @@ TimeZoneGenericNames::createInstance(const Locale& locale, UErrorCode& status) { } // End of mutex locked block if (cacheEntry == nullptr) { - delete instance; return nullptr; } instance->fRef = cacheEntry; - return instance; + return instance.orphan(); } bool