Tuesday, December 7, 2010

Memory Leak Detection and Isolation - Part2: Set Breakpoints on a Memory Allocation Number

Tip - To identify which allocation in code causing memory allocation even we know line number and function name.
Details - Sometimes you know the line number and function name of the code which cause memory leak. But it can be executed several times and often it is difficult to track which and when allocation cause memory leak. The piece of information that allows you to do this is the memory allocation number. This is the number that appears in braces after the filename and line number when those are displayed. For example, in the following output, the memory allocation number is 109. It means that the leaked memory is the 109th block of memory allocated in your program.
The CRT library counts all memory blocks allocated during execution of the program, including memory allocated by the CRT library itself or by other libraries such as MFC. Therefore, an object with allocation number N will be the Nth object allocated in your program but may not be the Nth object allocated by your code. (In most cases, it will not be.)
If you can set the break point at the memory allocation number in you program, your debugger can break the execution at particular break point and is able to debug wisely to track the memory leak.


Fortunately CRT library provides you the methods to set break point at  memory allocation number. Two methods are described below.

Using _CrtSetBreakAlloc():
Call _CrtSetBreakAlloc(AllocationNumber); in your code.

Using Watch window of visual studio debugger:
Type ‘_CrtSetBreakAlloc’ in the name field of watch window. Type ‘{,,msvcr71d.dll}_crtBreakAlloc’ for multithreaded dll version of CRT library(/MD option).
After debugger evaluate the expression, edit the value field with memory allocation number to set the breakpoint.




Reference    :

Posted By :Krishnaraj S.

No comments:

Post a Comment