Why is cout.imbue (locale ("")) causing a memory leak?

My compiler is Visual VC ++ 2013. The following simple program will cause several memory leaks.

Why? How to fix it?

#define _CRTDBG_MAP_ALLOC #include <stdlib.h> #include <crtdbg.h> #include <cstdlib> #include <iostream> #include <locale> using namespace std; int main() { _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF); cout.imbue(locale("")); // If this statement is commented, then OK. } 

The debug window appears as follows:

 Detected memory leaks! Dumping objects -> {387} normal block at 0x004FF8C8, 12 bytes long. Data: <zh - CN > 7A 00 68 00 2D 00 43 00 4E 00 00 00 {379} normal block at 0x004FF678, 12 bytes long. Data: <zh - CN > 7A 00 68 00 2D 00 43 00 4E 00 00 00 {352} normal block at 0x004FE6E8, 12 bytes long. Data: <zh - CN > 7A 00 68 00 2D 00 43 00 4E 00 00 00 {344} normal block at 0x004FE498, 12 bytes long. Data: <zh - CN > 7A 00 68 00 2D 00 43 00 4E 00 00 00 {318} normal block at 0x004FD5C8, 12 bytes long. Data: <zh - CN > 7A 00 68 00 2D 00 43 00 4E 00 00 00 {308} normal block at 0x004F8860, 12 bytes long. Data: <zh - CN > 7A 00 68 00 2D 00 43 00 4E 00 00 00 Object dump complete. Detected memory leaks! Dumping objects -> {387} normal block at 0x004FF8C8, 12 bytes long. Data: <zh - CN > 7A 00 68 00 2D 00 43 00 4E 00 00 00 {379} normal block at 0x004FF678, 12 bytes long. Data: <zh - CN > 7A 00 68 00 2D 00 43 00 4E 00 00 00 {352} normal block at 0x004FE6E8, 12 bytes long. Data: <zh - CN > 7A 00 68 00 2D 00 43 00 4E 00 00 00 {344} normal block at 0x004FE498, 12 bytes long. Data: <zh - CN > 7A 00 68 00 2D 00 43 00 4E 00 00 00 {318} normal block at 0x004FD5C8, 12 bytes long. Data: <zh - CN > 7A 00 68 00 2D 00 43 00 4E 00 00 00 {308} normal block at 0x004F8860, 12 bytes long. Data: <zh - CN > 7A 00 68 00 2D 00 43 00 4E 00 00 00 Object dump complete. The program '[0x5B44] cpptest.exe' has exited with code 0 (0x0). 
+6
source share
1 answer

I used std::codecvt and got a similar problem. I am not sure if this is the same reason. Just try to provide a possible way to discover the root cause.

You can reference the example at http://www.cplusplus.com/reference/locale/codecvt/in/

This is actually a “use” member of mylocale , and it seems without a link version of the overload of the R-value. Therefore, when writing directly, const facet_type& myfacet = std::use_facet<facet_type>(std::locale()); the same problem may occur ..

So try

 auto myloc = locale(""); cout.imbue(myloc); 
+4
source

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


All Articles