Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why will cout.imbue(locale("")) cause memory leaks?

My compiler is Visual VC++ 2013. The following simplest program will cause a few 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 outputs as follows:

Detected memory leaks!
Dumping objects ->
{387} normal block at 0x004FF8C8, 12 bytes long.
 Data: <z h - C N   > 7A 00 68 00 2D 00 43 00 4E 00 00 00 
{379} normal block at 0x004FF678, 12 bytes long.
 Data: <z h - C N   > 7A 00 68 00 2D 00 43 00 4E 00 00 00 
{352} normal block at 0x004FE6E8, 12 bytes long.
 Data: <z h - C N   > 7A 00 68 00 2D 00 43 00 4E 00 00 00 
{344} normal block at 0x004FE498, 12 bytes long.
 Data: <z h - C N   > 7A 00 68 00 2D 00 43 00 4E 00 00 00 
{318} normal block at 0x004FD5C8, 12 bytes long.
 Data: <z h - C N   > 7A 00 68 00 2D 00 43 00 4E 00 00 00 
{308} normal block at 0x004F8860, 12 bytes long.
 Data: <z h - C N   > 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: <z h - C N   > 7A 00 68 00 2D 00 43 00 4E 00 00 00 
{379} normal block at 0x004FF678, 12 bytes long.
 Data: <z h - C N   > 7A 00 68 00 2D 00 43 00 4E 00 00 00 
{352} normal block at 0x004FE6E8, 12 bytes long.
 Data: <z h - C N   > 7A 00 68 00 2D 00 43 00 4E 00 00 00 
{344} normal block at 0x004FE498, 12 bytes long.
 Data: <z h - C N   > 7A 00 68 00 2D 00 43 00 4E 00 00 00 
{318} normal block at 0x004FD5C8, 12 bytes long.
 Data: <z h - C N   > 7A 00 68 00 2D 00 43 00 4E 00 00 00 
{308} normal block at 0x004F8860, 12 bytes long.
 Data: <z h - C N   > 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).
like image 608
xmllmx Avatar asked Nov 16 '13 16:11

xmllmx


People also ask

What causes memory leaks C++?

Memory leaks occur when new memory is allocated dynamically and never deallocated. In C programs, new memory is allocated by the malloc or calloc functions, and deallocated by the free function. In C++, new memory is usually allocated by the new operator and deallocated by the delete or the delete [] operator.

Why do memory leaks happen?

In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations in a way that memory which is no longer needed is not released. A memory leak may also happen when an object is stored in memory but cannot be accessed by the running code.

Are memory leaks permanent C++?

Memory leaks don't result in physical or permanent damage. Since it's a software issue, it will slow down the applications or even your whole system. However, a program taking up a lot of RAM space doesn't always mean its memory is leaking somewhere. The program you're using may really need that much space.

What are memory leaks in coding?

DEFINITION A memory leak is the gradual deterioration of system performance that occurs over time as the result of the fragmentation of a computer's RAM due to poorly designed or programmed applications that fail to free up memory segments when they are no longer needed.


1 Answers

I was using std::codecvt and get a similar problem. I am not sure whether it is a same cause. Just try to provide s possible way to discover the root cause.

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

It actually "use" the member of mylocale, and it seems without an r-value reference version overload. So when directly write const facet_type& myfacet = std::use_facet<facet_type>(std::locale()); may cause the same problem. .

So try

auto myloc = locale("");
cout.imbue(myloc);
like image 56
Cigany Avatar answered Sep 28 '22 23:09

Cigany