Skip to content

Commit

Permalink
ICU-22830 Fix memLeak in numrange_fluent.cpp
Browse files Browse the repository at this point in the history
Maybe related to the leak in ICU-22800
  • Loading branch information
FrankYFTang committed Aug 6, 2024
1 parent 575bb78 commit 09ae314
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions icu4c/source/i18n/numrange_fluent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,8 @@ FormattedNumberRange LocalizedNumberRangeFormatter::formatFormattableRange(
return FormattedNumberRange(U_ILLEGAL_ARGUMENT_ERROR);
}

auto* results = new UFormattedNumberRangeData();
if (results == nullptr) {
status = U_MEMORY_ALLOCATION_ERROR;
LocalPointer<UFormattedNumberRangeData> results(new UFormattedNumberRangeData(), status);
if (U_FAILURE(status)) {
return FormattedNumberRange(status);
}

Expand All @@ -333,9 +332,8 @@ FormattedNumberRange LocalizedNumberRangeFormatter::formatFormattableRange(

// Do not save the results object if we encountered a failure.
if (U_SUCCESS(status)) {
return FormattedNumberRange(results);
return FormattedNumberRange(results.orphan());
} else {
delete results;
return FormattedNumberRange(status);
}
}
Expand Down Expand Up @@ -373,27 +371,21 @@ LocalizedNumberRangeFormatter::getFormatter(UErrorCode& status) const {
}

// Try computing the formatter on our own
auto* temp = new NumberRangeFormatterImpl(fMacros, status);
LocalPointer<NumberRangeFormatterImpl> temp(new NumberRangeFormatterImpl(fMacros, status), status);
if (U_FAILURE(status)) {
delete temp;
return nullptr;
}
if (temp == nullptr) {
status = U_MEMORY_ALLOCATION_ERROR;
return nullptr;
}

// Note: ptr starts as nullptr; during compare_exchange,
// it is set to what is actually stored in the atomic
// if another thread beat us to computing the formatter object.
auto* nonConstThis = const_cast<LocalizedNumberRangeFormatter*>(this);
if (!nonConstThis->fAtomicFormatter.compare_exchange_strong(ptr, temp)) {
if (!nonConstThis->fAtomicFormatter.compare_exchange_strong(ptr, temp.getAlias())) {
// Another thread beat us to computing the formatter
delete temp;
return ptr;
} else {
// Our copy of the formatter got stored in the atomic
return temp;
return temp.orphan();
}

}
Expand Down

0 comments on commit 09ae314

Please sign in to comment.