From f904dc9003534aeccb5a94429a439a83e8f9472f Mon Sep 17 00:00:00 2001 From: Michael Ow Date: Wed, 1 Oct 2008 21:17:55 +0000 Subject: [PATCH] ICU-6554 Add releasing of allocated memory when uprv_tzname is called. X-SVN-Rev: 24698 --- icu4c/source/common/putil.c | 14 ++++++++++++++ icu4c/source/common/putilimp.h | 8 +++++++- icu4c/source/i18n/timezone.cpp | 4 ++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/icu4c/source/common/putil.c b/icu4c/source/common/putil.c index 88c51f32ff..d31d24d555 100644 --- a/icu4c/source/common/putil.c +++ b/icu4c/source/common/putil.c @@ -761,6 +761,7 @@ static const char* remapShortTimeZone(const char *stdID, const char *dstID, int3 static char* defaultTZBuffer = NULL; static int64_t defaultTZFileSize = 0; static FILE* defaultTZFilePtr = NULL; +static UBool defaultTZstatus = FALSE; /* * This method compares the two files given to see if they are a match. * It is currently use to compare two TZ files. @@ -937,6 +938,7 @@ uprv_tzname(int n) defaultTZFileSize = 0; if (gTimeZoneBufferPtr != NULL && isValidOlsonID(gTimeZoneBufferPtr)) { + defaultTZstatus = TRUE; return gTimeZoneBufferPtr; } #endif @@ -980,6 +982,18 @@ uprv_tzname(int n) #endif } +U_CAPI void U_EXPORT2 +uprv_free_tzname(void) +{ + /* Only care if memory allocation due to searching for system timezone file. */ + if (defaultTZstatus == TRUE) { + uprv_free(gTimeZoneBufferPtr); + gTimeZoneBufferPtr = NULL; + defaultTZstatus = FALSE; + } +} + + /* Get and set the ICU data directory --------------------------------------- */ static char *gDataDirectory = NULL; diff --git a/icu4c/source/common/putilimp.h b/icu4c/source/common/putilimp.h index c516821e84..a9e31d0fa0 100644 --- a/icu4c/source/common/putilimp.h +++ b/icu4c/source/common/putilimp.h @@ -1,7 +1,7 @@ /* ****************************************************************************** * -* Copyright (C) 1997-2006, International Business Machines +* Copyright (C) 1997-2008, International Business Machines * Corporation and others. All Rights Reserved. * ****************************************************************************** @@ -211,6 +211,12 @@ U_INTERNAL int32_t U_EXPORT2 uprv_timezone(void); */ U_INTERNAL const char* U_EXPORT2 uprv_tzname(int n); +/** + * If needed, frees the allocated pointer when tzname is called. + * @internal + */ +U_INTERNAL void U_EXPORT2 uprv_free_tzname(void); + /** * Get UTC (GMT) time measured in milliseconds since 0:00 on 1/1/1970. * @return the UTC time measured in milliseconds diff --git a/icu4c/source/i18n/timezone.cpp b/icu4c/source/i18n/timezone.cpp index 5a75b76ad0..aeaa7c3dc5 100644 --- a/icu4c/source/i18n/timezone.cpp +++ b/icu4c/source/i18n/timezone.cpp @@ -420,6 +420,10 @@ TimeZone::TimeZone(const UnicodeString &id) TimeZone::~TimeZone() { + /* When uprv_tzname() is called, memory might have been + allocated for the default timezone name. Release this + when the timezone object is deleted. */ + uprv_free_tzname(); } // -------------------------------------