HOWTO: Use _crtBreakAlloc to Debug a Memory AllocationLast reviewed: October 7, 1997Article ID: Q151585 |
The information in this article applies to:
SUMMARYWhen tracking down memory leaks using the debug C-Runtime (CRT), it is often useful to set a breakpoint immediately before allocating the memory that causes the leak. By setting _crtBreakAlloc at either compile time or run-time, you can cause a user-defined breakpoint at a specific point of memory allocation.
MORE INFORMATIONWhen tracking memory leaks with Debug-CRT functions, such as _CrtDumpMemoryLeaks, an allocation number enclosed in braces ({}) often appears. For example, the following is a memory leak at allocation number 18:
Detected memory leaks! Dumping objects -> {18} normal block at 0x00660BE4, 10 bytes long Data: < > CD CD CD CD CD CD CD CD CD CD Object dump complete.It is useful to set a breakpoint right before this memory gets allocated so you can step through the callstack and see what functions are causing this memory to get allocated. The Debug-CRT function _CrtSetBreakAlloc that allows you to specify an allocation number at which to break. This method requires that you recompile your program every time you want to set a allocation breakpoint. An alternative method is to use the Watch window and set the allocation breakpoint dynamically. This method has the advantage of not requiring any source code changes or recompiling. If you are statically linking to the C Run-time, the variable you want to change is called _crtBreakAlloc. If you are dynamically linking to the C Run-time, the variable you want to change in the Watch window is {,,msvcr40d.dll}*__p__crtBreakAlloc()if you are using Visual C++ 4.0 or 4.1. The variable you want to change in the Watch window should be {,,msvcrtd.dll}*__p__crtBreakAlloc() if you are using Visual C++ 4.2 or later. To determine which version of the CRT you are compiling with:
To set a allocation breakpoint dynamically, perform the following steps:
Keywords : CRTIss WBDebug Version : WINNT:4.0,4.1,4.2,5.0; Platform : NT WINDOWS Issue type : kbhowto |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |