DOCUMENT:Q140858 03-AUG-2001 [visualc] TITLE :PRB: _CRTDBG_MAP_ALLOC Does Not Work as Documented PRODUCT :Microsoft C Compiler PROD/VER:WINDOWS NT:4.0,4.1,5.0; OPER/SYS: KEYWORDS:kbdocerr kbVC400 kbVC410 kbVC500 kbVC600 kbprb ====================================================================== ------------------------------------------------------------------------------- The information in this article applies to: - The C Run-Time (CRT), included with: - Microsoft Visual C++, 32-bit Editions, versions 4.0, 4.1 - Microsoft Visual C++, 32-bit Enterprise Edition, version 5.0 - Microsoft Visual C++, 32-bit Professional Edition, version 5.0 - Microsoft Visual C++, 32-bit Enterprise Edition, version 6.0 - Microsoft Visual C++, 32-bit Professional Edition, version 6.0 - Microsoft Visual C++, 32-bit Learning Edition, version 6.0 ------------------------------------------------------------------------------- SYMPTOMS ======== When an object is allocated through use of the New operator and dumped through use of the debugging routines in the C Run-Time Library, the allocation is reported as occurring in the Crtdbg.h file line 512. CAUSE ===== This is caused by the definition of the overloaded operator New in the Crtdbg.h file: #ifdef _CRTDBG_MAP_ALLOC inline void* __cdecl operator new(unsigned int s) { return ::operator new(s, _NORMAL_BLOCK, __FILE__, __LINE__); } #endif /* _CRTDBG_MAP_ALLOC */ Here __FILE__ and __LINE__ are macros defined by the compiler that report the current file name and line number. Macros are filled out by the preprocessor. Then the compiler replaces your call to New with this function. Therefore, the macros have already been filled out before they are inlined. Hence they will report the header file information. MORE INFORMATION ================ The Books Online section titled "Using the Debug Heap from C++" in the Run-Time Library Reference states that defining the _CRTDBG_MAP_ALLOC symbol causes all instances of New in your code to be mapped properly to the debug version of New so as to record source file and line number information. While it is true that this will map calls to the debug version of New, it will not store the proper source file or line number information. There are two ways to mark the correct file name and line number: - Call the debug version of the new operator directly -or- - Create macros that replace the operator new in debug mode as in the following sample code. Sample Code ----------- /* MyDbgNew.h /* Defines global operator new to allocate from /* client blocks */ #ifdef _DEBUG #define MYDEBUG_NEW new( _NORMAL_BLOCK, __FILE__, __LINE__) // Replace _NORMAL_BLOCK with _CLIENT_BLOCK if you want the //allocations to be of _CLIENT_BLOCK type #else #define MYDEBUG_NEW #endif // _DEBUG /* MyApp.cpp /* Compile options needed: /Zi /D_DEBUG /MLd /* or use a /* Default Workspace for a Console Application to /* build a Debug version */ #include "crtdbg.h" #include "mydbgnew.h" #ifdef _DEBUG #define new MYDEBUG_NEW #endif void main( ) { char *p1; p1 = new char[40]; _CrtMemDumpAllObjectsSince( NULL ); } Additional query words: kbCRT kbOLDocs kbDSupport ====================================================================== Keywords : kbdocerr kbVC400 kbVC410 kbVC500 kbVC600 kbprb Technology : kbVCsearch kbAudDeveloper kbCRT Version : WINDOWS NT:4.0,4.1,5.0; Issue type : kbprb ============================================================================= 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. Copyright Microsoft Corporation 2001.