From fb0a19bbaf72ced7148df8b99f78d44fc7f0edb7 Mon Sep 17 00:00:00 2001 From: George Rhoten Date: Wed, 26 Nov 2003 22:57:27 +0000 Subject: [PATCH] ICU-813 Fix multithreaded issues and memory leak introduced by using gBogus. Now use a class local variable fBogus for bogus strings. This follows the DecimalFormatSymbols style. X-SVN-Rev: 13898 --- icu4c/source/i18n/fmtable.cpp | 20 ++++++++++++-------- icu4c/source/i18n/unicode/fmtable.h | 8 ++++---- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/icu4c/source/i18n/fmtable.cpp b/icu4c/source/i18n/fmtable.cpp index 3905ffd3fa7..26d3c955806 100644 --- a/icu4c/source/i18n/fmtable.cpp +++ b/icu4c/source/i18n/fmtable.cpp @@ -28,8 +28,6 @@ U_NAMESPACE_BEGIN UOBJECT_DEFINE_RTTI_IMPLEMENTATION(Formattable) -UnicodeString* Formattable::gBogus = NULL; - // ------------------------------------- // default constructor. // Creates a formattable object with a long value 0. @@ -46,6 +44,7 @@ Formattable::Formattable() Formattable::Formattable(UDate date, ISDATE /*isDate*/) : UObject(), fType(kDate) { + fBogus.setToBogus(); fValue.fDate = date; } @@ -55,6 +54,7 @@ Formattable::Formattable(UDate date, ISDATE /*isDate*/) Formattable::Formattable(double value) : UObject(), fType(kDouble) { + fBogus.setToBogus(); fValue.fDouble = value; } @@ -64,6 +64,7 @@ Formattable::Formattable(double value) Formattable::Formattable(int32_t value) : UObject(), fType(kLong) { + fBogus.setToBogus(); fValue.fInt64 = value; } @@ -73,6 +74,7 @@ Formattable::Formattable(int32_t value) Formattable::Formattable(int64_t value) : UObject(), fType(kInt64) { + fBogus.setToBogus(); fValue.fInt64 = value; } @@ -82,6 +84,7 @@ Formattable::Formattable(int64_t value) Formattable::Formattable(const char* stringToCopy) : UObject(), fType(kString) { + fBogus.setToBogus(); fValue.fString = new UnicodeString(stringToCopy); } @@ -91,6 +94,7 @@ Formattable::Formattable(const char* stringToCopy) Formattable::Formattable(const UnicodeString& stringToCopy) : UObject(), fType(kString) { + fBogus.setToBogus(); fValue.fString = new UnicodeString(stringToCopy); } @@ -101,6 +105,7 @@ Formattable::Formattable(const UnicodeString& stringToCopy) Formattable::Formattable(UnicodeString* stringToAdopt) : UObject(), fType(kString) { + fBogus.setToBogus(); fValue.fString = stringToAdopt; } @@ -109,6 +114,7 @@ Formattable::Formattable(UnicodeString* stringToAdopt) Formattable::Formattable(const Formattable* arrayToCopy, int32_t count) : UObject(), fType(kArray) { + fBogus.setToBogus(); fValue.fArrayAndCount.fArray = createArrayCopy(arrayToCopy, count); fValue.fArrayAndCount.fCount = count; } @@ -119,6 +125,7 @@ Formattable::Formattable(const Formattable* arrayToCopy, int32_t count) Formattable::Formattable(const Formattable &source) : UObject(source), fType(kLong) { + fBogus.setToBogus(); *this = source; } @@ -221,6 +228,7 @@ void Formattable::dispose() case kDate: case kDouble: case kLong: + case kInt64: break; } } @@ -462,13 +470,9 @@ Formattable::getArray(int32_t& count, UErrorCode* status) const // Gets the bogus string, ensures mondo bogosity. UnicodeString* -Formattable::getBogus() +Formattable::getBogus() const { - if (gBogus == NULL) { - gBogus = new UnicodeString(); - } - gBogus->setToBogus(); - return gBogus; + return (UnicodeString*)&fBogus; /* cast away const :-( */ } #if 0 diff --git a/icu4c/source/i18n/unicode/fmtable.h b/icu4c/source/i18n/unicode/fmtable.h index d9994368b28..2028f970db9 100644 --- a/icu4c/source/i18n/unicode/fmtable.h +++ b/icu4c/source/i18n/unicode/fmtable.h @@ -432,8 +432,7 @@ private: */ static Formattable* createArrayCopy(const Formattable* array, int32_t count); - static UnicodeString* gBogus; - static UnicodeString* getBogus(); + UnicodeString* getBogus() const; // Note: For now, we do not handle unsigned long and unsigned // double types. Smaller unsigned types, such as unsigned @@ -445,12 +444,13 @@ private: UDate fDate; struct { - Formattable* fArray; - int32_t fCount; + Formattable* fArray; + int32_t fCount; } fArrayAndCount; } fValue; Type fType; + UnicodeString fBogus; // Bogus string when it's needed. }; inline Formattable*