最遇到了一个可能内存泄漏问题,根据任务管理器目测,半小时涨了303k内存,感觉上是有内存泄漏,位置未知。
学习了下不通过第三方插件查找内存泄漏的方法。
原理就是通过#define 重定义 new 操作,纪录new操作和delete操作是否匹配
实现如下:
#define CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#ifdef _DEBUG
#ifndef DBG_NEW
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
#define new DBG_NEW
#endif
#endif // _DEBUG
class give_away
{
public:
give_away()
{
auto p_ele = new int[10];
_give = new int;
}
~give_away()
{
delete _give;
}
int *_give;
};
int main()
{
give_away* give = new give_away();
delete give;
_CrtDumpMemoryLeaks(); // 在需要查内存泄漏的地方,增加该函数
return 0;
}
在debug调试模式下,运行结果如下:
Detected memory leaks!
Dumping objects ->
c:\users\lj\source\repos\test\te