在vs2010中检测 C++ 程序内存泄露

来源:互联网 发布:tgp网络出口不稳定 编辑:程序博客网 时间:2024/06/11 05:30
C++ 没有垃圾回收,所以难免产生内存泄露,今天找到了一个办法 来检查程序中的内存泄露。解决办法:
//在程序中加入:#ifdef _DEBUG#define _CRTDBG_MAP_ALLOC#include <crtdbg.h>#define DEBUG_CLIENTBLOCK   new( _CLIENT_BLOCK, __FILE__, __LINE__)#define new DEBUG_CLIENTBLOCKstatic void Exit(){ int i = _CrtDumpMemoryLeaks(); //assert( i == 0);}#define CheckMemoryLeaks() {atexit(Exit);}#else#define DEBUG_CLIENTBLOCK#define CheckMemoryLeaks();#endif//测试 函数void MemoryLeak(){ char *c = new char [1024]();}int main(){   MemoryLeak(); CheckMemoryLeaks(); return 0;}

 

 

程序调试的out 里边会有 memory leak的提示,而且可以提示是哪一行。

使用dubug模式 运行

参考网址
http://my.oschina.net/u/131904/blog/41579
http://my.oschina.net/mutour/blog/113958
http://msdn.microsoft.com/zh-cn/library/e5ewb1h3%28v=vs.90%29.aspx

 

原创粉丝点击