選單

VC++中y一個檢測記憶體洩漏的類

VC++中y一個檢測記憶體洩漏的類

可利用CMemoryState類的物件來檢查:

在ProjectNameApp。cpp定義全域性變數:

static CMemoryState oldstate, newstate, diffstate;

在InitInstance()函式中新增如下程式碼:

oldstate。Checkpoint();

在ExitInstance()中新增如下程式碼:

newstate。Checkpoint();

if (diffstate。Difference(oldstate, newstate))

{

TRACE(“Memory leaked\n”);

diffstate。DumpStatistics();

}

return 0;

輸出如下形式:

Memory leaked。

0 bytes int 0 Free Blocks。

-626 bytes in -13 Normal Blocks。

0 bytes in 0 CRT Blocks。

0 bytes in 0 Ignore Blocks。

-1212 bytes in -6 Client Blockss。

Largest number used:178 bytes。

total allocations:988 bytes。