windows環境下記憶體泄露的檢查

來源:互聯網
上載者:User

參考文獻:

主要:http://www.cnblogs.com/dhbzzz/archive/2009/06/04/1495973.html

其他:http://www.oschina.net/question/161952_24831

http://msdn.microsoft.com/zh-cn/library/e5ewb1h3(v=vs.80).aspx

http://topic.csdn.net/u/20100530/19/3be8559f-824d-4d7e-8175-697992dfe7e0.html

linux上有valgrind,windows上有什麼工具??


方法:

stdafx.h裡面加入:

#define _CRTDBG_MAP_ALLOC 

#include<stdlib.h> 

#include<crtdbg.h>

_CRTDBG_MAP_ALLOC 定義一定要加上,帶上它可以讓你明確的看到是哪個檔案哪一行出現記憶體流失;

然後在工程的每個cpp檔案的#include 部分之後加上:

#ifdef _DEBUG 

#define new DEBUG_NEW 

#endif

程式的入口處加上

_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

該語句在程式退出時自動調用 _CrtDumpMemoryLeaks。

遇到的問題:

按照“方法”中做了,但是編譯出現:error C2065: “DEBUG_NEW”: 未聲明的標識符

解決方案:是要加上include<afx.h>這個標頭檔,因為這個標頭檔裡面才有定義DEBUG_NEW


結果:

輸出視窗顯示

Detected memory leaks!
Dumping objects ->
e:\lab\task2-opt mshog\mshogdlltest\mshogdlltest\mshogdlltest.cpp(15) : {116} normal block at 0x003BF5F0, 40 bytes long.
 Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD 
Object dump complete.


2012-05-18

xjs.xjtu@gmail.com

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.