zoukankan      html  css  js  c++  java
  • c++内存泄漏处理(积累)

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

    #include <crtdbg.h>
    #ifdef _DEBUG
    #define DEBUG_CLIENTBLOCK   new(_CLIENT_BLOCK, __FILE__, __LINE__)
    #define new DEBUG_CLIENTBLOCK
    #else
    #define DEBUG_CLIENTBLOCK
    #endif

    以下给出一段測试的代码

    test.h

    #include <memory.h>
    
    void funNew();
    test.cpp

    #include "text.h"
    
    #include <crtdbg.h>
    #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 <iostream>
    #include <assert.h>
    #include "text.h"
    
    #include <crtdbg.h>
    #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	est.cpp(13) : {157} client block at 0x001E9180, subtype 0, 4 bytes long.
     Data: <    > 00 00 00 00 
    f:project1main.cpp(17) : {156} client block at 0x001E9140, subtype 0, 4 bytes long.
     Data: <    > 00 00 00 00 
    Object dump complete.






  • 相关阅读:
    自己搭建二维码接口
    HTML CSS SPRITE 工具
    Codeforces Round #636 (Div. 3) 题解
    Codeforces Round #612 (Div. 1+Div. 2)
    计树问题小结 version 2.0
    Educational Codeforces Round 85 (Rated for Div. 2) 题解
    luogu6078 [CEOI2004]糖果
    luogu [JSOI2012]分零食
    多项式全家桶
    生成函数小结
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5096951.html
Copyright © 2011-2022 走看看