Microsoft KB Archive/130827

= FIX: MFC Reports Memory Leaks When cout and cin Are Used =

Article ID: 130827

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 1.0 Professional Edition

 Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.51

 Microsoft Visual C++ 1.52 Professional Edition</li></ul>

 Microsoft Visual C++ 1.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.1</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q130827

<div class="symptoms_section">

SYMPTOMS
An application that statically links to MFC and uses one of the C++ I/O functions (cout, cin, cerr, or clog) generates memory leak messages when it exits from the program. The messages look similar to this one: <pre class="fixed_text">  Detected memory leaks! Dumping objects -> {3} non-object block at $002F05CC, 512 bytes long {1} non-object block at $002F051C, 84 bytes long Object dump complete. The second line indicates a loss of 84 bytes for 32-bit editions and 28 bytes for 16-bit editions of Visual C++.

<div class="cause_section">

CAUSE
By referencing cout, cin, cerr, or clog you are causing these objects to do a one-time allocation for their file-buffer objects. The file-buffer objects are allocated using "operator new," and when you use MFC, these allocations are mapped through MFC's debug allocator (an overridden version of "operator new" that can be found in \MSVC\MFC\SRC\AFXMEM.CPP). There is also a memory buffer 512 bytes long allocated by the Run-Time library.

Because these objects are allocated by the Run-Time Library, they are freed by the Run-Time Library. However MFC's memory leak detection occurs before the Run-Time Library has destroyed its global objects, so it appears to MFC that there was an allocation made that has not been properly freed.

NOTE: This problem doesn't occur when the DLL version of the C/C++ Run-Time Library is used (MSVCRT20.DLL) because the memory allocations come from within the DLL and are not mapped through MFC's diagnostic allocator in the first place.

<div class="resolution_section">

RESOLUTION
These memory leaks can be safely ignored. The memory is correctly freed by the Run-Time Library.

<div class="status_section">

STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This problem was fixed in Microsoft Visual C++, 32-bit Edition, version 4.0.

Additional query words: 1.00 1.50 2.50 1.51 2.51 1.52 2.52 1.10 2.10 2.00 3.00 3.10

Keywords: kbbug kbfix KB130827

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.