When 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, you can cause a user-defined breakpoint at a specific point of memory allocation.
When 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.We can use the Watch window and set the allocation breakpoint dynamically. This method has the advantage of not requiring any source code changes or recompiling.
To set a allocation breakpoint dynamically, perform the following steps:
1. | Start your debugging session. From the Build menu, choose Debug -> Step-Into. If you are using the "Debug Single-Threaded" or "Debug Multi- Threaded" option , follow step 1a. Otherwise, follow step 1b.
| ||||
2. | Double click on the -1 value, and enter the new allocation number that causes a user-defined breakpoint. | ||||
3. | From the Debug menu, choose Debug -> Go Now the application will cause an user breakpoint just before allocating memory corresponding to the specified allocation number. |
For more information about _crtBreakAlloc, please search "Tracking Heap Allocation Requests" in MSDN.