From 8f7065db65d6208195b122c63a986926a3834662 Mon Sep 17 00:00:00 2001 From: Travis Keep Date: Mon, 10 Feb 2014 22:52:01 +0000 Subject: [PATCH] ICU-10640 Fix memory leak in adoptNumberFormat code. X-SVN-Rev: 35116 --- icu4c/source/i18n/measfmt.cpp | 6 ++++-- icu4c/source/i18n/reldatefmt.cpp | 7 ++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/icu4c/source/i18n/measfmt.cpp b/icu4c/source/i18n/measfmt.cpp index aed1ed66f76..c9a10685c73 100644 --- a/icu4c/source/i18n/measfmt.cpp +++ b/icu4c/source/i18n/measfmt.cpp @@ -579,6 +579,7 @@ void MeasureFormat::initMeasureFormat( NumberFormat *nfToAdopt, UErrorCode &status) { static const char *listStyles[] = {"unit", "unit-short", "unit-narrow"}; + LocalPointer nf(nfToAdopt); if (U_FAILURE(status)) { return; } @@ -597,7 +598,7 @@ void MeasureFormat::initMeasureFormat( return; } pluralRules->removeRef(); - if (nfToAdopt == NULL) { + if (nf.getAlias() == NULL) { SharedObject::copyPtr( NumberFormat::createSharedInstance( locale, UNUM_DECIMAL, status), @@ -607,7 +608,7 @@ void MeasureFormat::initMeasureFormat( } numberFormat->removeRef(); } else { - adoptNumberFormat(nfToAdopt, status); + adoptNumberFormat(nf.orphan(), status); if (U_FAILURE(status)) { return; } @@ -623,6 +624,7 @@ void MeasureFormat::initMeasureFormat( void MeasureFormat::adoptNumberFormat( NumberFormat *nfToAdopt, UErrorCode &status) { if (U_FAILURE(status)) { + delete nfToAdopt; return; } SharedNumberFormat *shared = new SharedNumberFormat(nfToAdopt); diff --git a/icu4c/source/i18n/reldatefmt.cpp b/icu4c/source/i18n/reldatefmt.cpp index 0e09b0dcfd1..99f463d0e77 100644 --- a/icu4c/source/i18n/reldatefmt.cpp +++ b/icu4c/source/i18n/reldatefmt.cpp @@ -620,6 +620,7 @@ UnicodeString& RelativeDateTimeFormatter::combineDateAndTime( void RelativeDateTimeFormatter::init( const Locale &locale, NumberFormat *nfToAdopt, UErrorCode &status) { + LocalPointer nf(nfToAdopt); if (!getFromCache(locale.getName(), cache, status)) { return; } @@ -631,7 +632,7 @@ void RelativeDateTimeFormatter::init( return; } pluralRules->removeRef(); - if (nfToAdopt == NULL) { + if (nf.getAlias() == NULL) { SharedObject::copyPtr( NumberFormat::createSharedInstance( locale, UNUM_DECIMAL, status), @@ -641,12 +642,12 @@ void RelativeDateTimeFormatter::init( } numberFormat->removeRef(); } else { - SharedNumberFormat *shared = new SharedNumberFormat(nfToAdopt); + SharedNumberFormat *shared = new SharedNumberFormat(nf.getAlias()); if (shared == NULL) { status = U_MEMORY_ALLOCATION_ERROR; - delete nfToAdopt; return; } + nf.orphan(); SharedObject::copyPtr(shared, numberFormat); } } -- 2.40.0