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.
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.
If you use the development tools of windows platform vs.
You can simply test for memory leaks in the following way.
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