VS检查内存泄漏的一种方法
#define _CRTDBG_MAP_ALLOC #include <cstdlib> #include <crtdbg.h> int main() { int* p = (int*)malloc(sizeof(int)*10); _CrtDumpMemoryLeaks(); return 0; }
可以输出未释放的内存的行号
//#define _CRTDBG_MAP_ALLOC//#define _DEBUG#include <cstdlib>#include <crtdbg.h>//#include "Test.cpp"////#ifdef _DEBUG// #define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )// // Replace _NORMAL_BLOCK with _CLIENT_BLOCK if you want the// // allocations to be of _CLIENT_BLOCK type//#else// #define DBG_NEW new//#endif//int a = 1;struct Pod { int x;};
int main() { //Pod* pPod = DBG_NEW Pod; //pPod = DBG_NEW Pod; // Oops, leaked the original pPod! //delete pPod;int* p = (int*)malloc(sizeof(int)*10); _CrtDumpMemoryLeaks(); return 0;}