gpt4 book ai didi

c++ - icu::Calendar::createInstance() 泄漏内存

转载 作者:行者123 更新时间:2023-11-30 01:53:48 27 4
gpt4 key购买 nike

这是一个泄漏的测试程序(在 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 说:

==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

我的问题是,我是否错误地使用了 ICU,或者是否有一些解决方法可以应用以释放 ICU 似乎正在创建的默认时区对象?我无法轻易更改我正在使用的操作系统和 ICU 的版本。

最佳答案

您需要调用u_cleanup() 来清理ICU。通常这无关紧要,所以不会自动完成。如果您使用 --enable-auto-cleanup 构建 ICU,它会在某些平台上尝试在库卸载时进行清理。参见 the userguide .

关于c++ - icu::Calendar::createInstance() 泄漏内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22801843/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com