设为首页 加入收藏

TOP

c++内存泄漏处理(积累)
2015-07-20 17:58:09 来源: 作者: 【 】 浏览:2
Tags:内存 泄漏 处理 积累

写c++程序时,经常会出现内存泄漏的问题,这里从网上找了一种很麻烦的方法:如果想找到每个cpp文件的内存泄漏,都必须在每个cpp加上如下代码:

#include 
  
   
#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK   new(_CLIENT_BLOCK, __FILE__, __LINE__)
#define new DEBUG_CLIENTBLOCK
#else
#define DEBUG_CLIENTBLOCK
#endif
  

下面给出一段测试的代码

test.h

#include 
  
   

void funNew();
  
test.cpp

#include "text.h"

#include 
  
   
#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK   new(_CLIENT_BLOCK, __FILE__, __LINE__)
#define new DEBUG_CLIENTBLOCK
#else
#define DEBUG_CLIENTBLOCK
#endif

void funNew()
{
	int *p = new int();
}
  
main.cpp

#include 
  
   
#include 
   
     #include "text.h" #include 
    
      #ifdef _DEBUG #define DEBUG_CLIENTBLOCK new(_CLIENT_BLOCK, __FILE__, __LINE__) #define new DEBUG_CLIENTBLOCK #else #define DEBUG_CLIENTBLOCK #endif int main() { _CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); int* p = new int(); funNew(); //delete p; return 0; }
    
   
  

输出

Detected memory leaks!
Dumping objects ->
f:\project1\test.cpp(13) : {157} client block at 0x001E9180, subtype 0, 4 bytes long.
 Data: <    > 00 00 00 00 
f:\project1\main.cpp(17) : {156} client block at 0x001E9140, subtype 0, 4 bytes long.
 Data: <    > 00 00 00 00 
Object dump complete.






】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU4925-Apple Tree 下一篇HDU 1695 GCD 欧拉函数+容斥原理+..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: