什么是内存泄漏?app
内存泄漏(memory leak),指因为疏忽或错误形成程序未能释放已经再也不使用的内存的状况。内存泄漏并不是指内存在物理上的消失,而是应用程序分配某段内存后,因为设计错误,失去了对该段内存的控制,于是形成了内存的浪费。编辑器
C和C++内存泄露函数
对于C和C++这种没有Garbage Collection 的语言来说,咱们主要关注两种类型的内存泄漏:编码
堆内存泄漏(Heap leak)。对内存指的是程序运行中根据须要经过malloc,realloc, new等从堆中分配的一块内存,完成后必须经过调用对应的 free或者delete 释放掉。若是程序设计的错误致使这部份内存没有被释放,那么此后这块内存将不会被使用,就会产生Heap Leak.spa
系统资源泄露(Resource Leak)。主要指程序使用系统分配的资源好比 Bitmap,Handle,SOCKET等没有使用相应的函数释放掉,致使系统资源的浪费,严重可致使系统效能下降,系统运行不稳定。设计
如何解决内存泄露?调试
内存泄露的问题其困难在于:code
一、编译器不能发现这些问题。对象
二、运行时才能捕获到这些错误,这些错误没有明显的症状,时隐时现。blog
三、对于手机等终端开发用户来讲,尤其困难。
下面从三个方面来解决内存泄露:
第一,良好的编码习惯,尽可能在涉及内存的程序段,检测出内存泄露。当程序稳定以后,再来检测内存泄露时,无疑增长了排除的难度和复杂度。
使用了内存分配的函数,要记得要使用其释放函数释放掉。
Heap memory:
malloc\realloc ------ free
new \new[] ---------- delete \delete[]
GlobalAlloc------------GlobalFree
Resource Leak :
对于系统资源使用以前要仔细看其使用说明,防止错误使用或者忘记释放掉系统资源。
对于基于引用计数的系统对象尤为要注意,由于只有其引用计数为0时,该对象才能正确被删除。而其使用过程当中又生成的新的系统资源,使用完毕后,若是没有及时删除,都会影响其引用计数。
使用Visual Leak Detector for Visual C++
简介
Visual Leak Detector是一款免费的、健全的、开源的Visual C++内存泄露检测系统。相比Visual C++自带的内存检测机制,Visual Leak Detector能够显示致使内存泄露的完整内存分配调用堆栈。
使用方法
VLD简单易用,文档也很丰富,对于内存泄露的具体位置也能以调用堆栈的形式详细的显示出来。
下载安装后,仅仅须要告诉Visual C++在哪里找到include文件和lib文件。
C/C++ -> General -> Additional Include Directories = C:\Program Files (x86)\Visual Leak Detector\include
Linker -> General -> Additional Library Directories = C:\Program Files (x86)\Visual Leak Detector\lib\Win32
Linker -> Input-> Additional Dependencies = vld.lib
而后,在代码上的变更就只须要简单的加上#include <vld.h> 就能够了。
当你在Visual C++中调试运行你的程序时,在程序结束后VLD将在output窗口输出一个内存泄露报告,该报告包括了你的程序中内存分配的完成调用栈信息。双击调用栈的某一行,就能够迅速跳转到你的代码编辑器相应的位置。
Visual Leak Detector有一些配置项,能够设置内存泄露报告的保存地(文件、调试器),拷贝"\Visual Leak Detector"路径下的vld.ini文件到工程的Debug目录下(在IDE运行的话,则须要拷贝到工程目录下),修改如下项:
ReportFile = .\memory_leak_report.txt
ReportTo = both
能够看到在Debug目录下生成文件memory_leak_report.txt,跟VS中Output窗口输出的内容同样。
总之,VLD是一种高效的诊断、修复C/C++程序内存泄露的方法。
应用举例
#include "stdafx.h" #ifdef _DEBUG //在Release模式下,不会连接Visual Leak Detector #include "vld.h" #endif int _tmain(int argc, _TCHAR* argv[]) { char* ch1 = new char[100]; char* ch2 = (char*)malloc(200); /* if (ch1 != NULL) { delete[] ch1; ch1 = NULL; } if (ch2 != NULL) { free(ch2); ch1 = NULL; } */ return 0; }
Output输出:
Visual Leak Detector Version 2.2.3 installed.
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x008CD6F8: 100 bytes ----------
Call Stack:
c:\users \consoleapplication\consoleapplication.cpp (12): ConsoleApplication.exe!wmain + 0x7 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (533): ConsoleApplication.exe!__tmainCRTStartup + 0x19 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (377): ConsoleApplication.exe!wmainCRTStartup
0x74EE8543 (File and line number not available): KERNEL32.DLL!BaseThreadInitThunk + 0xE bytes
0x770EAC69 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x85 bytes
0x770EAC3C (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x58 bytes
Data:
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD ........ ........
---------- Block 2 at 0x008CD798: 200 bytes ----------
Call Stack:
c:\users\ \consoleapplication\consoleapplication.cpp (13): ConsoleApplication.exe!wmain + 0xD bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (533): ConsoleApplication.exe!__tmainCRTStartup + 0x19 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (377): ConsoleApplication.exe!wmainCRTStartup
0x74EE8543 (File and line number not available): KERNEL32.DLL!BaseThreadInitThunk + 0xE bytes
0x770EAC69 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x85 bytes
0x770EAC3C (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x58 bytes
Data:
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD ........ ........
Visual Leak Detector detected 2 memory leaks (372 bytes).
Largest number used: 372 bytes.
Total allocations: 372 bytes.
Visual Leak Detector is now exiting.
The program '[0x262C] ConsoleApplication1.exe' has exited with code 0 (0x0).
输出的部分主要分为两块
Call Stack部分:
是泄露内存的调用堆栈,其中显示了泄露资源建立的位置,双击即可以定位到相应的代码部分。
Data部分:
即泄露部分的内存内容。