设置全局区域设置时的内存泄漏 [英] Memory leak when setting global locale

查看:184
本文介绍了设置全局区域设置时的内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码设置我的语言环境:

  locale :: global(locale(pt_BR.UTF-8 )); 

我这样做是否正确?当我使用 Valgrind 运行代码时,它显示内存泄漏。我应该如何处理?
我应该重置旧的区域设置吗?

  376字节在1块仍然可以在损失记录65中73 
== 7536 == at 0x4C2AB80:malloc(在/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so中)
== 7536 == by 0x5380664:duplocale(duplocale.c:53)
== 7536 == by 0x4EA436F:std :: ctype< wchar_t> :: ctype(__ locale_struct *,unsigned long)(在/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
== 7536 == by 0x4EA86B3:std :: locale :: _ Impl :: _ Impl(char const *,unsigned long)(在/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
== 7536 == by 0x4EA8BDA:std :: locale :: locale(char const *)(在/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
= = 7536 == by 0x40D68D:Relatorio :: geraRelatorios()(Relatorio.cpp:23)
== 7536 == by 0x402335:main(main.cpp:24)
== 7536 ==
== 7536 == 1个块中的376个字节在丢失记录中仍然可达73
== 7536 == at 0x4C2AB80:malloc(在/ usr / lib / valgrind / vgpreload_memcheck-amd64-linux中)。 so)
== 7536 == by 0x5380664:duplocale(duplocale.c:53)
== 7536 == by 0x4EA16DF:std :: codecvt< wchar_t,char,__mbstate_t> :: codecvt(__ locale_struct *,unsigned long)(在/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
== 7536 == by 0x4EA86E1:std :: locale :: _ Impl :: _ Impl(char const *,unsigned long)(在/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
== 7536 == by 0x4EA8BDA:std :: locale :: locale(char const * )(在/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
== 7536 == by 0x40D68D:Relatorio :: geraRelatorios()(Relatorio.cpp:23)
== 7536 == by 0x402335:main(main.cpp:24)
== 7536 ==
== 7536 == 1块中的376字节仍然可以在损失记录67中找到73
== 7536 == at 0x4C2AB80:malloc(在/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so中)
== 7536 == by 0x5380664:duplocale(duplocale.c:53)
== 7536 == by 0x4EA87D6:std :: locale :: _ Impl :: _ Impl(char const *,unsigned long)(在/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
== 7536 == by 0x4EA8BDA:std :: locale :: locale(char const *)(在/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
= = 7536 == by 0x40D68D:Relatorio :: geraRelatorios()(Relatorio.cpp:23)
== 7536 == by 0x402335:main(main.cpp:24)
== 7536 ==
== 7536 == 1个块中的376个字节仍然可以在丢失记录中找到68
== 7536 == at 0x4C2AB80:malloc(在/ usr / lib / valgrind / vgpreload_memcheck-amd64-linux中)。所以)
== 7536 == by 0x5380664:duplocale(duplocale.c:53)
== 7536 == by 0x4EB1E8D:std :: __ timepunct< wchar_t> :: _ M_initialize_timepunct(__ locale_struct *) /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
== 7536 == by 0x4ED6A27:std :: __ timepunct< wchar_t> :: __ timepunct(__ locale_struct *,char const *,unsigned long)(在/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
== 7536 == by 0x4EA890C:std :: locale :: _ Impl :: _ Impl(char const *, unsigned long)(在/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
== 7536 ==由0x4EA8BDA:std :: locale :: locale(char const *)(in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
== 7536 == by 0x40D68D:Relatorio :: geraRelatorios()(Relatorio.cpp:23)
== 7536 == by 0x402335:main(main.cpp:24)
== 7536 ==
== 7536 == 1块中的376字节在丢失记录中仍然可见73 73
== 7536 == at 0x4C2AB80:malloc(在/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so中)
== 7536 == by 0x5380664:duplocale(duplocale.c:53)
== 7536 == by 0x4ED71E0:std :: messages< wchar_t> :: messages(__ locale_struct *,char const *,unsigned long)(在/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
== 7536 == by 0x4EA899F:std :: locale :: _ Impl :: _ Impl(char const *,unsigned long)(在/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19 )
== 7536 == by 0x4EA8BDA:std :: locale :: locale(char const *)(在/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
== 7536 == by 0x40D68D:Relatorio :: geraRelatorios()(Relatorio.cpp:23)
== 7536 == by 0x402335:main(main.cpp:24)
== 7536 ==
== 7536 == 1个块中的400个字节在丢失记录中仍然可达73
== 7536 ==在0x4C2B0E0:operator new(unsigned long)(在/ usr / lib / valgrind / vgpreload_memcheck-amd64-linux.so)
== 7536 == by 0x4EB1C29:std :: __ timepunct< char> :: _ M_initialize_timepunct(__ locale_struct *)(在/usr/lib/x86_64-linux-gnu/libstdc++.so .6.0.19)
== 7536 == by 0x4EBBC47:std :: __ timepunct< char> :: __ timepunct(__ locale_struct *,char const *,unsigned long)(在/ usr / lib / x86_64-linux-gnu /libstdc++.so.6.0.19)
== 7536 == by 0x4EA85F2:std :: locale :: _ Impl :: _ Impl(char const *,unsigned long)(在/ usr / lib / x86_64-linux- gnu / libstdc ++。so.6.0.19)
== 7536 == by 0x4EA8BDA:std :: locale :: locale(char const *)(在/usr/lib/x86_64-linux-gnu/libstdc++.so .6.0.19)
== 7536 == by 0x40D68D:Relatorio :: geraRelatorios()(Relatorio.cpp:23)
== 7536 == by 0x402335:main(main.cpp:24)
== 7536 ==
== 7536 == 1个块中的400个字节在丢失记录71中仍然可达73
== 7536 == at 0x4C2B0E0:operator new(unsigned long) (在/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
== 7536 == by 0x4EB25A9:std :: __ timepunct< wchar_t> :: _ M_initialize_timepunct(__ locale_struct *)(在/ usr / lib / x86_64-linux-gnu / libstdc ++。so.6.0.19)
== 7536 == by 0x4ED6A27:std :: __ timepunct< wchar_t> :: __ timepunct(__ locale_struct *,char const *,unsigned long) usr / lib / x86_64-linux-gnu / libstdc ++。so.6.0.19)
== 7536 == by 0x4EA890C:std :: locale :: _ Impl :: _ Impl(char const *,unsigned long) /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
== 7536 == by 0x4EA8BDA:std :: locale :: locale(char const *)(在/ usr / lib / x86_64-linux-gnu / libstdc ++。so.6.0.19)
== 7536 == by 0x40D68D:Relatorio :: geraRelatorios()(Relatorio.cpp:23)
== 7536 == by 0x402335 :main(main.cpp:24)
== 7536 ==
== 7536 == 1块中的576字节在丢失记录中仍然可达72 73
== 7536 ==在0x4C2B0E0:operator new(unsigned long)(在/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so中)
== 7536 == by 0x4EA8384:std :: locale :: _ Impl :: _ Impl(char const *,unsigned long)(在/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
== 7536 == by 0x4EA8BDA:std :: locale :: locale(char const * )(在/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
== 7536 == by 0x40D68D:Relatorio :: geraRelatorios()(Relatorio.cpp:23)
== 7536 == by 0x402335:main(main.cpp:24)
== 7536 ==
== 7536 == 1块中的1,344字节仍然可在损失记录73中到达73
== 7536 == at 0x4C2B0E0:operator new(unsigned long)(在/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so中)
== 7536 == by 0x4EA86A1:std :: locale :: _ Impl :: _ Impl(char const *,unsigned long)(在/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
== 7536 == by 0x4EA8BDA:std :: locale :: locale(char const *)(在/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
== 7536 == by 0x40D68D:Relatorio :: geraRelatorios()(Relatorio .cpp:23)
== 7536 == by 0x402335:main(main.cpp:24)


$ b b

我只是调用locale一次,给我一百个内存泄漏。



谢谢。



在gcc(v5.3.0)的源代码中,构造函数locale :: locale(在libstdc ++ - v3 / src / c ++ 98 / localename.cc)使用 new _Impl(...)(_ M_impl = _S_classic) > _M_add_reference(); 。而destructor(在locale.cc)调用 _M_remove_reference 减少ref计数器。所以我不知道为什么会有内存泄漏。



读了locale :: locale的构造函数后,我发现了一个诀窍。离开应用程序时,调用 locale :: global(locale(C)); 。使用 locale(C)不要在构造函数中使用新的_M_add_reference。因此,以前的语言环境( locale(pt_BR.UTF-8)将被销毁,新的语言环境将不会通过分配创建。


I am using this code to set my locale:

locale::global(locale("pt_BR.UTF-8"));

Am I doing this correctly ? When I run the code with Valgrind, it is displaying memory leak. How should I handle with ? Should I reset the old locale ?

376 bytes in 1 blocks are still reachable in loss record 65 of 73
==7536==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7536==    by 0x5380664: duplocale (duplocale.c:53)
==7536==    by 0x4EA436F: std::ctype<wchar_t>::ctype(__locale_struct*, unsigned long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==7536==    by 0x4EA86B3: std::locale::_Impl::_Impl(char const*, unsigned long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==7536==    by 0x4EA8BDA: std::locale::locale(char const*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==7536==    by 0x40D68D: Relatorio::geraRelatorios() (Relatorio.cpp:23)
==7536==    by 0x402335: main (main.cpp:24)
==7536== 
==7536== 376 bytes in 1 blocks are still reachable in loss record 66 of 73
==7536==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7536==    by 0x5380664: duplocale (duplocale.c:53)
==7536==    by 0x4EA16DF: std::codecvt<wchar_t, char, __mbstate_t>::codecvt(__locale_struct*, unsigned long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==7536==    by 0x4EA86E1: std::locale::_Impl::_Impl(char const*, unsigned long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==7536==    by 0x4EA8BDA: std::locale::locale(char const*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==7536==    by 0x40D68D: Relatorio::geraRelatorios() (Relatorio.cpp:23)
==7536==    by 0x402335: main (main.cpp:24)
==7536== 
==7536== 376 bytes in 1 blocks are still reachable in loss record 67 of 73
==7536==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7536==    by 0x5380664: duplocale (duplocale.c:53)
==7536==    by 0x4EA87D6: std::locale::_Impl::_Impl(char const*, unsigned long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==7536==    by 0x4EA8BDA: std::locale::locale(char const*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==7536==    by 0x40D68D: Relatorio::geraRelatorios() (Relatorio.cpp:23)
==7536==    by 0x402335: main (main.cpp:24)
==7536== 
==7536== 376 bytes in 1 blocks are still reachable in loss record 68 of 73
==7536==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7536==    by 0x5380664: duplocale (duplocale.c:53)
==7536==    by 0x4EB1E8D: std::__timepunct<wchar_t>::_M_initialize_timepunct(__locale_struct*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==7536==    by 0x4ED6A27: std::__timepunct<wchar_t>::__timepunct(__locale_struct*, char const*, unsigned long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==7536==    by 0x4EA890C: std::locale::_Impl::_Impl(char const*, unsigned long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==7536==    by 0x4EA8BDA: std::locale::locale(char const*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==7536==    by 0x40D68D: Relatorio::geraRelatorios() (Relatorio.cpp:23)
==7536==    by 0x402335: main (main.cpp:24)
==7536== 
==7536== 376 bytes in 1 blocks are still reachable in loss record 69 of 73
==7536==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7536==    by 0x5380664: duplocale (duplocale.c:53)
==7536==    by 0x4ED71E0: std::messages<wchar_t>::messages(__locale_struct*, char const*, unsigned long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==7536==    by 0x4EA899F: std::locale::_Impl::_Impl(char const*, unsigned long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==7536==    by 0x4EA8BDA: std::locale::locale(char const*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==7536==    by 0x40D68D: Relatorio::geraRelatorios() (Relatorio.cpp:23)
==7536==    by 0x402335: main (main.cpp:24)
==7536== 
==7536== 400 bytes in 1 blocks are still reachable in loss record 70 of 73
==7536==    at 0x4C2B0E0: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7536==    by 0x4EB1C29: std::__timepunct<char>::_M_initialize_timepunct(__locale_struct*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==7536==    by 0x4EBBC47: std::__timepunct<char>::__timepunct(__locale_struct*, char const*, unsigned long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==7536==    by 0x4EA85F2: std::locale::_Impl::_Impl(char const*, unsigned long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==7536==    by 0x4EA8BDA: std::locale::locale(char const*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==7536==    by 0x40D68D: Relatorio::geraRelatorios() (Relatorio.cpp:23)
==7536==    by 0x402335: main (main.cpp:24)
==7536== 
==7536== 400 bytes in 1 blocks are still reachable in loss record 71 of 73
==7536==    at 0x4C2B0E0: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7536==    by 0x4EB25A9: std::__timepunct<wchar_t>::_M_initialize_timepunct(__locale_struct*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==7536==    by 0x4ED6A27: std::__timepunct<wchar_t>::__timepunct(__locale_struct*, char const*, unsigned long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==7536==    by 0x4EA890C: std::locale::_Impl::_Impl(char const*, unsigned long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==7536==    by 0x4EA8BDA: std::locale::locale(char const*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==7536==    by 0x40D68D: Relatorio::geraRelatorios() (Relatorio.cpp:23)
==7536==    by 0x402335: main (main.cpp:24)
==7536== 
==7536== 576 bytes in 1 blocks are still reachable in loss record 72 of 73
==7536==    at 0x4C2B0E0: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7536==    by 0x4EA8384: std::locale::_Impl::_Impl(char const*, unsigned long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==7536==    by 0x4EA8BDA: std::locale::locale(char const*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==7536==    by 0x40D68D: Relatorio::geraRelatorios() (Relatorio.cpp:23)
==7536==    by 0x402335: main (main.cpp:24)
==7536== 
==7536== 1,344 bytes in 1 blocks are still reachable in loss record 73 of 73
==7536==    at 0x4C2B0E0: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7536==    by 0x4EA86A1: std::locale::_Impl::_Impl(char const*, unsigned long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==7536==    by 0x4EA8BDA: std::locale::locale(char const*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==7536==    by 0x40D68D: Relatorio::geraRelatorios() (Relatorio.cpp:23)
==7536==    by 0x402335: main (main.cpp:24)

I just call the locale once, and gives me a hundred of memory leak.

Thanks.

解决方案

I have the same problem.

In the source code of gcc (v5.3.0), the constructor locale::locale (in libstdc++-v3/src/c++98/localename.cc) uses new _Impl (…) or (_M_impl = _S_classic)->_M_add_reference();. And the destructor (in locale.cc) call _M_remove_reference that decrease the ref counter. So I don't know why there is a memory leak.

After reading the constructor of locale::locale, I found a trick. On leaving your application, call locale::global(locale("C"));. Using locale("C") do not use a new in constructor but a _M_add_reference. So the previous locale (locale("pt_BR.UTF-8") will be destroyed and the new locale will not be created by allocation.

这篇关于设置全局区域设置时的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆