search

Home  >  Q&A  >  body text

c++中如何检查内存泄漏

大家讲道理大家讲道理2804 days ago608

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 14:00:44

    If you use the development tools of windows platform vs.

    You can simply test for memory leaks in the following way.

    //定义宏
    #define _CRTDBG_MAP_ALLOC
    #include <stdlib.h>
    //在最后引入测试内存工具头文件。
    #include <crtdbg.h>
     
    
    using namespace std;
     
    
     
    int main(int argc,char** argv)
    {
        auto p =new char[1000];
        //此句放在结束测试的位置
        _CrtDumpMemoryLeaks();
        return 0;
    }

    The principle is that the memory application and release functions are mapped in this header file.
    In addition, there are other third-party tools such as UMDH, VLD, Purify, BoundsCheck, etc.


    Under linux

    There is also a tool Mtrace based on the same principle.
    There are also third-party tools, such as: valgrind etc.

    reply
    0
  • 迷茫

    迷茫2017-04-17 14:00:44

    1) Most memory leaks can be discovered and solved by reviewing the code.

    2) Pay attention to the life cycle of resources when developing and use RAII to manage resources.

    3) Use a tool (valgrind) to detect the memory allocation and recycling of the program.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 14:00:44

    The correct approach is to write safe code rather than making up for it afterwards. Go to C++11 and use smart pointers, so you no longer have to worry about forgetting to release pointers.

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 14:00:44

    I usually use valgrind

    reply
    0
  • Cancelreply