您的位置:首页 > 其它

调试 方便地检测程序的内存泄漏 CMemoryState

2013-02-21 12:49 309 查看
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

成员

void Checkpoint( );
获取一个快照(检查点)并将其存储
备注:成员函数 Difference 和 DumpAllObjectsSince 使用此快照数据。
 
BOOL Difference(
const CMemoryState& oldState,
const CMemoryState& newState
);
计算类型之间 CMemoryState两个对象的差异
非零,如果两个内存状态不同的;否则为0。
 
void DumpAllObjectsSince( ) const;
转储所有当前分配的对象摘要从以前的检查点。即打印检查点到当前位置的内存分配情况
 
void DumpStatistics( ) const;
打印 CMemoryState 对象的内存分配统计信息
备注:
    可用块
    普通块
    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-------------------

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: