Here's a test program which leaks (on Ubuntu 10.04, ICU 4.2):

#include <unicode/uversion.h>
#include <unicode/calendar.h>

int main()
{
  TimeZone* tz = TimeZone::createTimeZone("Asia/Pyongyang");

  UErrorCode status = U_ZERO_ERROR;
  Calendar* cal = Calendar::createInstance(tz, status); // adopts tz

  delete cal;
}

valgrind --leak-check=full says:

==22978== 304 bytes in 1 blocks are possibly lost in loss record 20 of 22
==22978==    at 0x4C28F86: malloc (vg_replace_malloc.c:291)
==22978==    by 0x526B34D: icu_4_2::UnicodeString::allocate(int) (in /usr/lib/libicuuc.so.42.1)
==22978==    by 0x526B993: icu_4_2::UnicodeString::cloneArrayIfNeeded(int, int, signed char, int**, signed char) (in /usr/lib/libicuuc.so.42.1)
==22978==    by 0x526D97A: icu_4_2::UnicodeString::doReplace(int, int, unsigned short const*, int, int) (in /usr/lib/libicuuc.so.42.1)
==22978==    by 0x4EF0E90: icu_4_2::TimeZone::initDefault() (in /usr/lib/libicui18n.so.42.1)
==22978==    by 0x4EF10A4: icu_4_2::TimeZone::createDefault() (in /usr/lib/libicui18n.so.42.1)
==22978==    by 0x4EEE750: icu_4_2::GregorianCalendar::GregorianCalendar(icu_4_2::Locale const&, UErrorCode&) (in /usr/lib/libicui18n.so.42.1)
==22978==    by 0x4EEB493: ??? (in /usr/lib/libicui18n.so.42.1)
==22978==    by 0x4EEBA01: icu_4_2::Calendar::createInstance(icu_4_2::TimeZone*, icu_4_2::Locale const&, UErrorCode&) (in /usr/lib/libicui18n.so.42.1)
==22978==    by 0x400A42: icu_4_2::Calendar::createInstance(icu_4_2::TimeZone*, UErrorCode&) (in /home/jzwinck/test/a.out)
==22978==    by 0x4009C5: main

My question is, am I using ICU incorrectly, or is there some workaround I can apply to free the default time zone object which ICU seems to be creating? I can't easily change the versions of OS and ICU I'm using.

有帮助吗?

解决方案

You need to call u_cleanup() to clean up ICU. Usually it doesn't matter, so is not done automatically. If you build ICU with --enable-auto-cleanup it will attempt to cleanup when the library unloads, on some platforms. See the userguide.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top