通过malloc分配序号查找内存泄漏

转载 2016年07月08日 14:59:07

The file name and line number in the memory leak report tell you where leaked memory is allocated, but knowing where the memory is allocated is not always sufficient to identify the problem. Often an allocation will be called many times during a run of the program, but it may leak memory only on certain calls. To identify the problem, you need to know not only where the leaked memory is allocated but also the conditions under which the leak occurs. 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 18. It means that the leaked memory is the 18th block of memory allocated in your program.

Detected memory leaks!
Dumping objects ->
C:\PROGRAM FILES\VISUAL STUDIO\MyProjects\leaktest\leaktest.cpp(20) : {18} 
normal block at 0x00780E80, 64 bytes long.
 Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.

The CRT library counts all memory blocks allocated during a run 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.)

You can use the allocation number to set a breakpoint at the location where memory is allocated. To do this, set a location breakpoint near the start of your program. When your program breaks at that point, you can set such a memory-allocation breakpoint from the QuickWatch dialog box or the Watch window.

Procedure

To set a memory-allocation breakpoint in the Watch window

  1. In the Watch window, type the following expression in the Name column:

    _crtBreakAlloc
    

    If you are using the multithreaded DLL version of the CRT library (the /MD option), include the context operator, as shown here:

    {,,msvcr90d.dll}_crtBreakAlloc
    
  2. Press RETURN.

    The debugger evaluates the call and places the result in the Value column. This value will be –1 if you have not set any breakpoints on memory allocations.

  3. Replace the value in the Value column with the allocation number of the memory allocation where you want to break. For example, 18 to break at the allocation shown in the output above.

After you set breakpoints on the memory allocations you are interested in, you can continue debugging. Be careful to run the program under the same conditions as the previous run so that the allocation order does not change. When your program breaks at the specified memory allocation, you can look at the Call Stack window and other debugger information to determine the conditions under which the memory was allocated. If necessary, you can continue execution of the program from that point to see what happens to the object and perhaps determine why it is not properly deallocated.

NoteNote:

Setting a data breakpoint on the object may be helpful. For more information, see How to: Set a Data Breakpoint (Native Only).

Although it is usually easier to set memory-allocation breakpoints in the debugger, you can set them in your code, if you prefer.

To set a memory-allocation breakpoint in your code

  • Add a line like this (for the 18th memory allocation):

    _crtBreakAlloc = 18;
    

Alternately, you can use the _CrtSetBreakAlloc function, which has the same effect:

_CrtSetBreakAlloc(18);

相关文章推荐

malloc内存泄漏检测

  • 2011年10月26日 16:06
  • 2KB
  • 下载

malloc内存泄漏

  • 2017年03月06日 09:53
  • 12KB
  • 下载

使用__wrap_malloc替换malloc查找内存泄漏

由于malloc_hook在多线程中不合用,因此转用__wrap_symbol方法支持多线程调用malloc。 缺点:所有source code都链接重编译。 ld中有一个选项–wrap,当查...
  • LBO4031
  • LBO4031
  • 2016年06月05日 23:58
  • 1325

重定义malloc和free检测内存泄漏(转)

1, 定义供应用程序使用的头文件 //libmem.h #ifndef _LIBMEM_H_ #define _LIBMEM_H_ //声明自定义malloc及free函数 extern...

关于C语言的malloc函数以及内存泄漏的问题

malloc函数 malloc函数的原型: (void *)malloc(int size)  malloc函数的返回值是一个void类型的指针,参数为int类型的数据,即申请分配的内存大小...

检测内存泄漏malloc模块

1)实际应用中,很多地方用到malloc动态申请内存,其实频繁使用应该没有什么碎片之类的影响,linux会帮我们优化,只要我们使用后正常free就ok了。2)检测思路是每次malloc申请的时候记录一...

linux中内存泄漏的检测(二)定制化的malloc/free

《linux中内存泄漏的检测(一)最简单的方法》介绍了最简单的内存泄漏检测方法,这种方法虽然简单,却有很多现实的问题,导致它不能用于实际的生产中。直接使用这种方法肯定是不现实的,因为:(1)把整个工程...

内存泄漏查找方法总结

  • 2013年07月01日 11:30
  • 38KB
  • 下载

JProfiler内存泄漏查找文档

  • 2009年12月10日 13:13
  • 180KB
  • 下载

Umdhtools.exe: 如何使用 umdh.exe 来查找内存泄漏

注意:这篇文章是由无人工介入的自动的机器翻译系统翻译完成。这些文章是微软为不懂英语的用户提供的, 以使他们能够理解这些文章的内容。微软不保证机器翻译的正确度,也不对由于内容的误译或者客户对它的使用所引...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:通过malloc分配序号查找内存泄漏
举报原因:
原因补充:

(最多只允许输入30个字)