Visual Studio 内存泄漏检测方法
生活随笔
收集整理的這篇文章主要介紹了
Visual Studio 内存泄漏检测方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Visual Studio 內存泄漏檢測方法 非MFC程序可以用以下方法檢測內存泄露: 1.程序開始包含如下定義: #ifdef _DEBUG #define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__) #else #define DEBUG_CLIENTBLOCK #endif // _DEBUG #define _CRTDBG_MAP_ALLOC #include <stdlib.h> #include <crtdbg.h> #ifdef _DEBUG #define new DEBUG_CLIENTBLOCK #endif // _DEBUG 2.程序中添加下面的函數: _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF); Debug版本程序運行結束后如有內存泄漏,輸出窗口中會顯示類似信息: Detected memory leaks! Dumping objects -> g:\programs\test\test.cpp(16) : {51} client block at 0x00385C58, subtype 0, 4 bytes long. Data: < ? ?> CD CD CD CD? Object dump complete. MFC程序內存泄漏檢測方法: 1.在 CMyApp 中添加如下三個 CMemoryState 類的成員變量: #ifdef _DEBUG protected: CMemoryState m_msOld, m_msNew, m_msDiff; #endif // _DEBUG 2.在 CMyApp::InitInstance() 中添加如下代碼: #ifdef _DEBUG m_msOld.Checkpoint(); #endif // _DEBUG 3.在 CMyApp::ExitInstance() 中添加如下代碼: #ifdef _DEBUG m_msNew.Checkpoint(); if (m_msDiff.Difference(m_msOld, m_msNew)) { afxDump<<"\nMemory Leaked :\n"; m_msDiff.DumpStatistics(); afxDump<<"Dump Complete !\n\n"; } #endif // _DEBUG Debug版本程序運行結束后如有內存泄漏,輸出窗口中會顯示類似信息: Memory Leaked : 0 bytes in 0 Free Blocks. 8 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: 8825 bytes. Total allocations: 47506 bytes. Dump Complete ! Detected memory leaks! Dumping objects -> g:\programs\chat\chatdlg.cpp(120) : {118} normal block at 0x00D98150, 8 bytes long. Data: < ? ? ? ?> A8 7F D9 00 01 00 00 00? Object dump complete.
轉載于:https://www.cnblogs.com/cplover/p/3372038.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Visual Studio 内存泄漏检测方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 工银教师白金信用卡额度是多少?起步价5万
- 下一篇: The connection to ad