扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
#ifdef _DEBUG //for memory leak check #define _CRTDBG_MAP_ALLOC //使生成的内存dump包含内存块分配的具体代码为止 #include<stdlib.h> #include<crtdbg.h> #endif |
//enable leak check _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG); |
HANDLE hLogFile;//声明日志文件句柄 hLogFile = CreateFile("./log/memleak.log", GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);//创建日志文件 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);//将warn级别的内容都输出到文件(注意dump的报告级别即为warning) _CrtSetReportFile(_CRT_WARN, hLogFile);//将日志文件设置为告警的输出文件 |
_CrtMemState s1, s2, s3;//定义3个临时内存状态 ...... _CrtDumpMemoryLeaks();//Dump从程序开始运行到该时刻点,已分配而未释放的内存,即前述An //以下部分非必要,仅为方便后续分析增加信息 _CrtMemCheckpoint( &s2 ); if ( _CrtMemDifference( &s3, &s1, &s2) ) { _CrtMemDumpStatistics( &s3 );//dump相邻时间点间的内存块变化 //for next compare _CrtMemCheckpoint( &s1 ); } time_t now = time(0); struct tm *nowTime = localtime(&now); _RPT4(_CRT_WARN,"%02d %02d:%02d:%02d snapshot dump.\n", nowTime->tm_mday, nowTime->tm_hour,nowTime->tm_min,nowTime->tm_sec);//输出该次dump时间 |
Detected memory leaks! Dumping objects -> {20575884} normal block at 0x05C4C490, 87 bytes long. Data: < > 02 00 1D 90 84 9F A6 89 00 00 00 00 00 00 00 00 ... d:\xxxxx\xxxworker.cpp(903) : {20575705} normal block at 0x05D3EF90, 256 bytes long. Data: < > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... Object dump complete. 0 bytes in 0 Free Blocks. 215968 bytes in 876 Normal Blocks. 0 bytes in 0 CRT Blocks. 0 bytes in 0 Ignore Blocks. 0 bytes in 0 Client Blocks. Largest number used: 220044 bytes. Total allocations: 7838322 bytes. 10 16:29:14 snapshot dump. |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者