title: Failure in freeing the heap
body: <p>I have a Win32 console application. When I run that code it throws this exception nearly 60% of the time.</p>
  
  <p><strong>Unhandled exception at 0x777BC799 (ntdll.dll) in x.exe: 0xC0000374: A heap has been corrupted (parameters: 0x777E8890).</strong></p>
  
  <pre><code>void function(CString &amp;outputStr, const char* name, DWORD64 value){
      outputStr = _T("");
      CString csName(name);
      outputStr.Format(_T("&lt;name=\"%s\" value=\"0x%08x\"/&gt;\n"), csName, value);
  }
  </code></pre>
  
  <p>This is one of the functions where the crash takes place(in the last line). When I see the stacktrace it is somewhat like this:</p>
  
  <pre><code>ntdll.dll!_RtlpHeapHandleError@4()  
  ntdll.dll!_RtlpLogHeapFailure@24()  
  ntdll.dll!@RtlpLowFragHeapFree@12() 
  ntdll.dll!_RtlFreeHeap@12() Unknown
  abc.exe!ATL::CWin32Heap::Free(void * p) Line 153    C++
  abc.exe!ATL::CAtlStringMgr::Free(ATL::CStringData * pData) Line 107 C++
  abc.exe!ATL::CStringData::Release() Line 92 C++
  abc.exe!ATL::CSimpleStringT&lt;char,0&gt;::~CSimpleStringT&lt;char,0&gt;() Line 263 C++
  abc.exe!ATL::CStringT&lt;char,ATL::StrTraitATL&lt;char,ATL::ChTraitsCRT&lt;char&gt; &gt;       ::~CStringT&lt;char,ATL::StrTraitATL&lt;char,ATL::ChTraitsCRT&lt;char&gt; &gt; &gt;() Line 1295C++
  </code></pre>
  
  <p>Another block of code where crash happen is</p>
  
  <pre><code>std::string dir = "";
  dir = dir + "\\" + "abc";
  </code></pre>
  
  <p>with a similar stack trace. Earlier by debugging I saw a pattern that the crash occurs only where string manipulation is taking place(CString or std::string). As shown in the stack, the crash is occurring when the Free function is called internally when returning from the function block. Now I don't know why this is happening.</p>