使用运行时API定位内存泄露
测试Demo一直负责解码图片和显示结果而没有整理清楚, 今天对程序退出时VC调试检测出的内存泄露进行定位和处理, 现在已经消除所有内存泄露问题,检测结果:
The thread 0xEA4 has exited with code 2 (0x2). Detected memory leaks! Dumping objects -> {183} normal block at 0x06A80040, 5570560 bytes long. Data: < > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 {182} normal block at 0x06920040, 1392672 bytes long. Data: < > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 {181} normal block at 0x067C0040, 1392672 bytes long. Data: <IEEJMLKMGFDA?=<<> 49 45 45 4A 4D 4C 4B 4D 47 46 44 41 3F 3D 3C 3C {180} normal block at 0x063C0040, 4177920 bytes long. Data: < > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 {179} normal block at 0x061C0040, 2088960 bytes long. Data: < > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 Object dump complete. The thread 0x778 has exited with code 2 (0x2).
VC提供运行时内存检测API---_CrtSetBreakAlloc, 这个函数功能是在指定申请内存次数时出发断点,例如上面出错结果中块{179}标定了179次内存申请没有得到释放.在主对话框OninitDialog初始化时加入
_CrtSetBreakAlloc(179);
则通过F5再调试时就能通过调用堆栈定位到是那一块内存申请, 进而核对有没有释放的地方.