c++中如何检查内存泄漏
大家讲道理
大家讲道理 2017-04-17 13:58:44
0
4
639
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(4)
洪涛

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.

迷茫

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.

洪涛

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.

巴扎黑

I usually use valgrind

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template