天天看點

調試 友善地檢測程式的記憶體洩漏 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-------------------