c++如何解决内存泄漏

Linxu

Linux系统下解决内存泄漏可以使用valgrind工具。

下载valgrind

sudo apt-get install valgrind

Linux下使用valgrind

 g++ -g -o app test.cpp 
 valgrind --leak-check=full ./app
代码如下
#include<iostream>
using namesapce std;
int main()
{
        int i = 0;
        int * intV = new int[10];
        for(int i = 0; i < 10; i++)
        {
                intV[i] = i;
        }
​
        for(int i = 0; i < 10; i++) {
                cout << intV[i] <<endl;
        }
​
        delete [] intV;
}

valgrind打印的泄漏信息

==5155== Memcheck, a memory error detector
==5155== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==5155== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==5155== Command: ./app
==5155== 
==5155== 
==5155== HEAP SUMMARY:
==5155==     in use at exit: 40 bytes in 1 blocks
==5155==   total heap usage: 3 allocs, 2 frees, 76,840 bytes allocated
==5155== 
==5155== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
==5155==    at 0x483C583: operator new[](unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==5155==    by 0x1091E5: main (test.cpp:7)
==5155== 
==5155== LEAK SUMMARY:
==5155==    definitely lost: 40 bytes in 1 blocks
==5155==    indirectly lost: 0 bytes in 0 blocks
==5155==      possibly lost: 0 bytes in 0 blocks
==5155==    still reachable: 0 bytes in 0 blocks
==5155==         suppressed: 0 bytes in 0 blocks
==5155== 
==5155== For lists of detected and suppressed errors, rerun with: -s
==5155== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

正常信息

==5719== 
==5719== HEAP SUMMARY:
==5719==     in use at exit: 0 bytes in 0 blocks
==5719==   total heap usage: 3 allocs, 3 frees, 73,768 bytes allocated
==5719== 
==5719== All heap blocks were freed -- no leaks are possible
==5719== 
==5719== For lists of detected and suppressed errors, rerun with: -s
==5719== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Windows

windows下qt可以使用qt自带的一个Leaker插件,安装后即可使用,但是需要进行收费,本文主要介绍VLD方式,通过vs进行内存检测。

首先下载VLD安装包

到GitHub下载最新的VLD程序Release v2.5.1 · KindDragon/vld (github.com)

会自动适配到本地安装的vs中,之后将里面的lib,include文件打包之后,把bin目录下的dll放到运行的bin目录下,之后配置头文件和库目录,加入#include "vld.h"头文件,程序启动会打印

Visual Leak Detector read settings from: D:\Program Files\Visual Leak Detector\vld.ini
Visual Leak Detector Version 2.5.1 installed.

代码如下:

#include <iostream>
using namespace std;

#include "vld.h"

void test()
{
	char* pChar = new char[20];
	//delete[] pChar;
	return;
}

int main()
{
	test();
	return 0;
}

如果存在内存泄漏,程序会输出

WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x00000000EDC96DE0: 20 bytes ----------
  Leak Hash: 0x5A484AE6, Count: 1, Total 20 bytes
  Call Stack (TID 6140):
    ucrtbased.dll!malloc()
    f:\dd\vctools\crt\vcstartup\src\heap\new_array.cpp (16): LeetCode.exe!operator new[]()
    d:\visualprograme\leetcode\leetcode\test2.cpp (8): LeetCode.exe!test() + 0xA bytes
    d:\visualprograme\leetcode\leetcode\test2.cpp (16): LeetCode.exe!main()
    f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (75): LeetCode.exe!invoke_main()
    f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (264): LeetCode.exe!__scrt_common_main_seh() + 0x5 bytes
    f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (309): LeetCode.exe!__scrt_common_main()
    f:\dd\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): LeetCode.exe!mainCRTStartup()
    KERNEL32.DLL!BaseThreadInitThunk() + 0x14 bytes
    ntdll.dll!RtlUserThreadStart() + 0x21 bytes
  Data:
    CD CD CD CD    CD CD CD CD    CD CD CD CD    CD CD CD CD     ........ ........
    CD CD CD CD                                                  ........ ........
​
Visual Leak Detector detected 1 memory leak (72 bytes).
Largest number used: 72 bytes.
Total allocations: 72 bytes.
Visual Leak Detector is now exiting.

如果不存在内存泄漏,则会打印如下信息

No memory leaks detected.
Visual Leak Detector is now exiting.

还可以将其输出到文件中

vld.ini参数设置说明 默认读取的是安装目录下的vld.ini 文件,当exe所在目录下也有vld.ini文件时会优先读取这份配置。 VLD:选择VLD的打开与关闭。在Debug模式下运行,关闭以后会有一行VLD关闭的提示信息。默认为 on。 AggregateDuplicates:设置为 yes 时,相同地方产生内存泄漏只输出一次,但是会统计发生的次数。默认是 no 。 MaxDataDump:输出的dump数据个数,默认为 256。 MaxTraceFrames:输出的调用栈的层数。默认是 64。 ReportEncoding :report 文件的编码格式,可选有 ascii, unicode,默认是 ascii 。 ReportFile :report 文件的路径。默认是 “.\memory_leak_report.txt” ReportTo :可选有 debugger, file, both,debugger 表示输出到 debug模式下的输出窗口;file 表示只输出到文件中; both顾名思义,全都都输出。默认是 debugger 。 备注:怎么让VLD的文件输出到一个独立的文件中? vld.ini参数设置说明 答:配置 ReportFile = .\你的程序名称_vldLog.txt 和 ReportTo = both 即可。

配置好路径之后

会在当前目录生成文件

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值