什么是内存泄漏?

内存泄漏(memory leak),指由于疏忽或错误造成程序未能释放已经不再使用的内存的情况。内存泄漏并非指内存在物理上的消失,而是应用程序分配某段内存后,由于设计错误,失去了对该段内存的控制,因而造成了内存的浪费。

CC++内存泄露

       对于C和C++这种没有Garbage Collection 的语言来讲,我们主要关注两种类型的内存泄漏:

堆内存泄漏(Heap leak)。对内存指的是程序运行中根据需要通过malloc,realloc, new等从堆中分配的一块内存,完成后必须通过调用对应的 free或者delete 释放掉。如果程序设计的错误导致这部分内存没有被释放,那么此后这块内存将不会被使用,就会产生Heap Leak.

系统资源泄露(Resource Leak)。主要指程序使用系统分配的资源比如 Bitmap,Handle,SOCKET等没有使用相应的函数释放掉,导致系统资源的浪费,严重可导致系统效能降低,系统运行不稳定。

如何解决内存泄露?

内存泄露的问题其困难在于:

1、编译器不能发现这些问题。

2、运行时才能捕获到这些错误,这些错误没有明显的症状,时隐时现。

3、对于手机等终端开发用户来说,尤为困难。

下面从三个方面来解决内存泄露:

第一,良好的编码习惯,尽量在涉及内存的程序段,检测出内存泄露。当程序稳定之后,再来检测内存泄露时,无疑增加了排除的难度和复杂度。

使用了内存分配的函数,要记得要使用其释放函数释放掉。

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[];
char* ch2 = (char*)malloc(); /*
if (ch1 != NULL)
{
delete[] ch1;
ch1 = NULL;
}
if (ch2 != NULL)
{
free(ch2);
ch1 = NULL;
}
*/ return ;
}

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部分:

即泄露部分的内存内容。

使用Visual Leak Detector for Visual C++ 捕捉内存泄露的更多相关文章

  1. 使用Visual Leak Detector检测内存泄漏[转]

      1.初识Visual Leak Detector 灵活自由是C/C++语言的一大特色,而这也为C/C++程序员出了一个难题.当程序越来越复杂时,内存的管理也会变得越加复杂,稍有不慎就会出现内存问题 ...

  2. vld(Visual Leak Detector) 内存泄露检测工具

    初识Visual Leak Detector 灵活自由是C/C++语言的一大特色,而这也为C/C++程序员出了一个难题.当程序越来越复 杂时,内存的管理也会变得越加复杂,稍有不慎就会出现内存问题.内存 ...

  3. Cocos开发中性能优化工具介绍之Visual Studio内存泄漏检测工具——Visual Leak Detector

    那么在Windows下有什么好的内存泄漏检测工具呢?微软提供Visual Studio开发工具本身没有什么太好的内存泄漏检测功能,我们可以使用第三方工具Visual Leak Detector(以下简 ...

  4. Visual Leak Detector 2.2.3 Visual C++内存检测工具

      Visual Leak Detector是一款免费的.健全的.开源的Visual C++内存泄露检测系统.相比Visual C++自带的内存检测机制,Visual Leak Detector可以显 ...

  5. Cocos性能优化工具的开发介绍Visual Studio内存泄漏检测工具——Visual Leak Detector

    然后,Windows下有什么好的内存泄漏检測工具呢?微软提供Visual Studio开发工具本身没有什么太好的内存泄漏检測功能.我们能够使用第三方工具Visual Leak Detector(下面简 ...

  6. VisualStudio 怎么使用Visual Leak Detector

    VisualStudio 怎么使用Visual Leak Detector 那么在Windows下有什么好的内存泄漏检测工具呢?微软提供Visual Studio开发工具本身没有什么太好的内存泄漏检测 ...

  7. Visual Leak Detector简明使用教程

    Visual Leak Detector是一款内存泄漏检测软件,主要的作用就是检测可能或者是存在内存泄露的地方,具体的功能的话,可以百度下,今天主要简单介绍下怎么使用 首先下载Visual Leak ...

  8. Visual Leak Detector原理剖析

    认识VLD VLD(Visual Leak Detector)是一款用于Visual C++的开源内存泄漏检测工具,我们只需要在被检测内存泄漏的工程代码里#include “vld.h”就可以开启内存 ...

  9. VS2017 编译 Visual Leak Detector + VLD 使用示例

    起因 一个Qt5+VS2017的工程,需要进行串口操作,在自动时发现一段时间软件崩溃了,没有保存log,在 debug 的时候发现每运行一次应用占据的内存就多一点,后来意识到是内存泄漏了.这个真是头疼 ...

随机推荐

  1. What's Security

    研究安全应该时常问自己这个问题,什么是安全,什么是安全,什么是安全,安全的本质是什么,只有理解了安全的本质,才能成功的设计一个安全方案. 现在的感觉是没有绝对的安全,所谓的'安全'其实都只不过是增加攻 ...

  2. ubuntu下面更改用户名的方法

    在装HADOOP 系统时候不小心, 没有将三台机器的用户名设置为一致的用户名,导致后面发生很多麻烦.下面总结一下UBUNTU中改用户名的方法. 1.  先给系统添加一个super用户,我们用这个用户名 ...

  3. Uncaught RangeError: Maximum call stack size exceeded 超出最大调用值(个人解释)

    写了段jq后,报这个错,度娘未解,灵光一闪,找到原因,上代码: Html 结构: <a href="javascript:;" class="item-pic&qu ...

  4. bzoj 1711 [Usaco2007 Open]Dining吃饭&&poj 3281 Dining

    最大流. 这东西好像叫三分图匹配. 源点向每个食物点连一条容量为1的边. 每个饮料点向汇点连一条容量为1的边. 将每个牛点拆点,食物点向喜欢它的牛的入点连一条容量为1的边,牛的出点向它喜欢的饮料点连一 ...

  5. 添加自编译的apache为linux系统服务

    步骤1添加服务脚本 #cp /app/apache2/bin/apachectl /etc/rc.d/init.d/httpd #ln -s /etc/rc.d/init.d/httpd /etc/r ...

  6. PostgreSQL

    PostgreSQL新手入门   作者: 阮一峰 日期: 2013年12月22日 自从MySQL被Oracle收购以后,PostgreSQL逐渐成为开源关系型数据库的首选. 本文介绍PostgreSQ ...

  7. Python 类(一)

    这一篇让先抽象的了解下类与实例 一类的定义 从具体的程序设计观点来看: 类是一种数据结构.我们可以使用类来定义包含数据值和行为特性的对象(类对象). 类是封装逻辑和数据的另一种形式. 面向对象设计观点 ...

  8. 使用Python解析JSON数据

    使用Python解析百度API返回的JSON格式的数据 # coding:utf-8 # !/usr/bin/env python import matplotlib.pyplot as plt fr ...

  9. Spring--多人开发

    在spring中, 可以定义多个配置文件. 例子: ①首先定义一个班级类和一个学生类 1 package com.fuwh.spring;      }                    }    ...

  10. Django基础,Day5 - form表单投票详解

    投票URL polls/urls.py: # ex: /polls/5/vote/ url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, ...