通过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);

c++ 内存泄漏检查

c++ 内存泄漏检查 转载自 http://www.cnblogs.com/skynet/archive/2011/02/20/1959162.html 内存泄漏指由于疏忽或错误造成程序未能...
  • ranran_5300
  • ranran_5300
  • 2016年02月26日 15:30
  • 308

Malloc是如何分配内存的?

在用户调用malloc之后,系统中发生了什么呢?
  • aganlengzi
  • aganlengzi
  • 2016年05月07日 19:15
  • 1912

用malloc动态分配二维数组

转自:http://blog.csdn.net/fqc0306/article/details/6686673 C语言提供类似于矩阵的多维数组,在一些进行数学运算的程序中会经常用到。...
  • qq1987924
  • qq1987924
  • 2015年01月05日 21:48
  • 1266

检测内存泄漏malloc模块

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

malloc()和free()函数的讲解以及相关内存泄漏问题

1、函数原型及说明: void *malloc(long NumBytes):该函数分配了NumBytes个字节,并返回了指向这块内存的指针。如果分配失败,则返回一个空指针(NULL)。 ...
  • doniexun
  • doniexun
  • 2014年07月30日 10:53
  • 1751

【基础】关于Linux平台malloc的写时拷贝(延迟分配)

Linux内核定义了“零页面”(内容全为0的一个物理页,且物理地址固定),应用层的内存分配请求,如栈扩展、堆分配、静态分配等,分配线性地址后,就将页表项条目指向“零页面”(指定初始值的情况除外),这样...
  • dongpy
  • dongpy
  • 2009年09月14日 17:26
  • 3453

malloc分配数组

malloc分配数组参考链接:https://zhidao.baidu.com/question/106461194.html#include #include int main() { i...
  • hduxiejun
  • hduxiejun
  • 2017年03月28日 16:49
  • 492

C语言中的malloc和free造成内存泄露?

      首先,说一下我对内存泄露的理解,内存泄露是指:程序中一块不再使用的内存没有被释放,造成内存保持占用状态,使操作系统不能将内存分配给其它的程序(进程)。在C语言,用的最多的管理内存的函数莫过...
  • leisure512
  • leisure512
  • 2009年11月08日 22:32
  • 6634

malloc能分配多大内存

《程序员自我修养》中的一段代码,贴出来看看效果
  • singleyellow
  • singleyellow
  • 2017年04月07日 18:02
  • 309

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

malloc函数 malloc函数的原型: (void *)malloc(int size)  malloc函数的返回值是一个void类型的指针,参数为int类型的数据,即申请分配的内存大小...
  • qq_26501341
  • qq_26501341
  • 2017年02月25日 11:43
  • 1272
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:通过malloc分配序号查找内存泄漏
举报原因:
原因补充:

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