Microsoft KB Archive/141493

= DOC: "How to Use Debug Heap from C++" Documented Incorrectly =

Article ID: 141493

Article Last Modified on 12/2/2003

-

APPLIES TO

 The C Run-Time (CRT), when used with:  Microsoft Visual C++ 4.0 Standard Edition

 Microsoft Visual C++ 4.1 Subscription

 Microsoft Visual C++ 4.2 Enterprise Edition

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

 Microsoft Visual C++ 5.0 Enterprise Edition</li></ul>

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

 Microsoft Visual C++ 6.0 Enterprise Edition</li></ul>

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

 Microsoft Visual C++ 6.0 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q141493

<div class="summary_section">

SUMMARY
In the Run-Time Library reference, the section on Using the Debug Heap from Visual C++ incorrectly describes how to place an allocation made with the new operator into a _CLIENT_BLOCK. To access this section from Books Online, follow this hierarchy:
 * 1) Visual C++ Books
 * 2) C/C++
 * 3) Run-Time Library Reference
 * 4) Debug Version of the C Run-Time Library
 * 5) Memory Management and the Debug Heap
 * 6) Using the Debug Heap from C++

<div class="moreinformation_section">

MORE INFORMATION
In a debug build of an application, you can use a special operator (new) to record the file name, the line number where the allocation occurred, and the block type of the allocation. The documentation states that if you want your allocations to be of type_CLIENT_BLOCK instead of _NORMAL_BLOCK you should include code like the following in an include file:
 * 1) ifdef _DEBUG

inline void* __cdecl operator new(unsigned int s)     { return ::operator new(s, _CLIENT_BLOCK, __FILE__, __LINE__); }

__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 not the actual source location.
 * 1) endif"

There are two ways to mark the correct file name and line number:  Call the debug version of the new operator directly

-or-

</li> Create macros that replace the operator new in debug mode as in the following sample code.</li></ul>

Sample Code
/* MyDbgNew.h /* Defines global operator new to allocate from /* client blocks #define DEBUG_CLIENTBLOCK  new( _CLIENT_BLOCK, __FILE__, __LINE__) #define DEBUG_CLIENTBLOCK
 * 1) ifdef _DEBUG
 * 1) ifdef _DEBUG
 * 1) else
 * 1) 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


 * 1) include "crtdbg.h"
 * 2) include "mydbgnew.h"


 * 1) ifdef _DEBUG
 * 2) define new DEBUG_CLIENTBLOCK
 * 3) endif

void main { char *p1; p1 = new char[40]; _CrtMemDumpAllObjectsSince( NULL ); }

Keywords: kbvc400 kbvc410 kbvc420 kbvc500 kbvc600 kbcrt kbdocerr kbcode KB141493

-

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

© Microsoft Corporation. All rights reserved.