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

Last reviewed: September 18, 1997
Article ID: Q130827
1.00 1.50 1.51 1.52 | 1.00 2.00 2.10
WINDOWS             | WINDOWS NT
kbprg kbfixlist

The information in this article applies to:

  • The Microsoft Foundation Classes (MFC) included with:

        - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, 1.52
        - Microsoft Visual C++, 32-bit Edition, versions 1.0, 2.0, 2.1
    

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:

   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++.

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.

RESOLUTION

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

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 reference words: 1.00 1.50 2.50 1.51 2.51 1.52 2.52 1.10
2.10 2.00 3.00 2.10 3.10
KBCategory: kbprg kbfixlist kbbuglist
KBSubcategory: MfcMisc
Keywords : MfcMisc kbbuglist kbfixlist kbprg
Technology : kbMfc
Version : 1.00 1.50 1.51 1.52 | 1.00 2.00
Platform : NT WINDOWS
Solution Type : kbfix


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 18, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.