Memory leak when configuring global locale

I use this code to set my locale:

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

Am I doing it right? When I run the code using Valgrind , it displays a memory leak. How can I handle this? Should I reset the old language?

 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 it gives me a hundred memory leaks.

Thanks.

+2
source share
2 answers

In the gcc source code (v5.3.0), the locale::locale constructor (in libstdc++-v3/src/c++98/localename.cc ) uses one of the following values:

  • new _Impl (…) : dynamic allocation, possible memory leak.
  • (_M_impl = _S_classic)->_M_add_reference(); : static link, memory leak.

The destructor locale::~locale (in libstdc++-v3/src/c++98/locale.cc ) calls _M_remove_reference (in libstdc++-v3/include/bits/locale_classes.h ), which checks the reference count and, if necessary, calls delete . Therefore, I do not know why a memory leak occurs.

After reading the constructor locale::locale , I found a trick. When exiting the application, call locale::global(locale("C")); . Using locale("C") does not use the new constructor in the constructor, but _M_add_reference .

Thus, the previous locale ( locale("pt_BR.UTF-8" ) will be destroyed, and the new locale will not be created by highlighting.

+1
source

Just stumbled upon this question.

For this reason, I wrote a small helper that provides a localized language. For code that uses locale settings, I set it at the beginning of main() if you check the code with valgrind. The locale from reset to "C" when returning main - at least in my development environments.

 struct scoped_locale_global { scoped_locale_global(const std::locale& loc) : m_prev_loc(std::locale::global(loc)) { } ~scoped_locale_global() { std::locale::global(m_prev_loc); } std::locale m_prev_loc; }; 
+1
source

Source: https://habr.com/ru/post/958208/


All Articles