天天看点

调试 方便地检测程序的内存泄漏 CMemoryStateCMemoryState结构

MSDN:http://msdn.microsoft.com/zh-cn/library/0wzsd007%28v=vs.110%29.aspx

参考的博客:http://blog.csdn.net/yuanweihuayan/article/details/7426412

CMemoryState结构

方便地检测到您的程序的内存泄漏。

struct CMemoryState      
成员      
备注:成员函数 Difference 和 DumpAllObjectsSince 使用此快照数据。      
BOOL Difference(
  const CMemoryState& oldState,
  const CMemoryState& newState 
);      
备注:      
    可用块

    普通块

    CRT 块

    忽略块

    客户端块。

    程序在任何时间使用的最大内存(以字节为单位)

    程序当前使用的总内存(以字节为单位)
      
例子:      
代码      
 CMemoryState oldMem, newMem, difMem; 
 oldMem.Checkpoint(); // 检测当前的内存使用情况
 char* c = new char[6];
 TRACE0("1-------------------\n");
 oldMem.DumpAllObjectsSince(); // oldMem就检测到这里 
 TRACE0("2-------------------\n");

 newMem.Checkpoint(); // 没有delete[] c
    if (difMem.Difference(oldMem, newMem)) // 比较

 {
  TRACE0("Memory Lack!\n");
 }

    TRACE0("3-------------------\n");
    difMem.DumpStatistics(); // 在Output中打印结果
    TRACE0("4-------------------\n"); 
      
输出      
1-------------------
Dumping objects ->
D:\20130204\testmfc\testmfcDlg.cpp(126) : {79} normal block at 0x005738A0, 6 bytes long.
 Data: <      > CD CD CD CD CD CD 
Object dump complete.
2-------------------
Memory Lack!
3-------------------
0 bytes in 0 Free Blocks.
6 bytes in 1 Normal Blocks.
0 bytes in 0 CRT Blocks.
0 bytes in 0 Ignore Blocks.
0 bytes in 0 Client Blocks.
Largest number used: 0 bytes.
Total allocations: 6 bytes.
4-------------------