admin管理员组文章数量:1531472
主要内容:
1、valgrind简介
2、一些参数信息
3、valgrind安装
4、valgrind简单使用
1、valgrind简介
valgrind是一套Linux下开源的仿真调试工具集。
它可以用于内存调试、内存泄漏检测以及性能分析。
2、一些参数信息
1、一些基本选项:
usage: valgrind [options] prog-and-args
tool-selection option, with default in [ ]:
--tool=<name> use the Valgrind tool named <name> [memcheck]
basic user options for all Valgrind tools, with defaults in [ ]:
-h --help show this message
--help-debug show this message, plus debugging options
--version show version
-q --quiet run silently; only print error msgs
-v --verbose be more verbose -- show misc extra info
--trace-children=no|yes Valgrind-ise child processes (follow execve)? [no]
--trace-children-skip=patt1,patt2,... specifies a list of executables
that --trace-children=yes should not trace into
--trace-children-skip-by-arg=patt1,patt2,... same as --trace-children-skip=
but check the argv[] entries for children, rather
than the exe name, to make a follow/no-follow decision
--child-silent-after-fork=no|yes omit child output between fork & exec? [no]
--vgdb=no|yes|full activate gdbserver? [yes]
full is slower but provides precise watchpoint/step
--vgdb-error=<number> invoke gdbserver after <number> errors [999999999]
to get started quickly, use --vgdb-error=0
and follow the on-screen directions
--vgdb-stop-at=event1,event2,... invoke gdbserver for given events [none]
where event is one of:
startup exit valgrindabexit all none
--track-fds=no|yes track open file descriptors? [no]
--time-stamp=no|yes add timestamps to log messages? [no]
--log-fd=<number> log messages to file descriptor [2=stderr]
--log-file=<file> log messages to <file>
--log-socket=ipaddr:port log messages to socket ipaddr:port
2、关于memcheck的选项:
user options for Memcheck:
--leak-check=no|summary|full search for memory leaks at exit? [summary]
--leak-resolution=low|med|high differentiation of leak stack traces [high]
--show-leak-kinds=kind1,kind2,.. which leak kinds to show?
[definite,possible]
--errors-for-leak-kinds=kind1,kind2,.. which leak kinds are errors?
[definite,possible]
where kind is one of:
definite indirect possible reachable all none
上面的kind是内存泄漏类型,如下:
definitely lost:确定泄露的内存,当程序结束时如果一块动态分配的内存没有被释放且通过程序内的指针变量均无法访问这块内存则会报这个错误。(经典的就是malloc、new,忘记free、delete)
indirectly lost:间接泄露的内存,和definitely lost一起出现,解决掉definitely lost即可恢复。当使用了含有指针成员的类或结构时可能会报这个错误
possibly lost:可能泄露的内存。当程序结束时如果一块动态分配的内存没有被释放且通过程序内的指针变量均无法访问这块内存的起始地址,但可以访问其中的某一部分数据,则会报这个错误。
still reachable:如果程序是正常结束的,那么它可能不会造成程序崩溃,但长时间运行有可能耗尽系统资源,如果程序是崩溃(如访问非法的地址而崩溃)而非正常结束的,则应当暂时忽略它,先修复导致程序崩溃的错误,然后重新检测。
suppressed:已被解决,出现了内存泄露但系统自动处理了。
3、valgrind安装
偷懒直接sudo apt install:
sudo apt install valgrind
或者官网(https://valgrind/downloads/current.html#current)下载,然后再tar解压,然后去valgrind目录下执行./configure(这儿得要有gcc,没有的话安装一下gcc),make,make install。
4、valgrind简单使用
默认情况下是使用的memcheck
如果要用其他功能的话,其实可以起别名,这样命令会简略很多,vim ~/.bashrc,加上别名:
alias memcheck='valgrind --tool=memcheck --leak-check=full'
最后source ~/.bashrc生效即可。
接下来我们来看例子:
1、动态申请内存,但没释放
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int *p = new int(2);
cout << "*p: " << *p <<endl;
return 0;
}
运行结果:
liugenyi@liugenyi-virtual-machine:~/linuxsty/cppstudy$ ./a.out
*p: 2
但是我们没有进行指针p的释放,我们用memcheck查看:2、越界访问:
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
int *p = new int[3];
p[3] = 10;
cout << "p[3]: " << p[3] << endl;
delete [] p;
return 0;
}
使用memcheck如下:3、使用没有初始化的内存:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int a[3];
int sum = 0;
a[0] = 0;
a[1] = 0;
sum = a[0] + a[1] + a[2];
if(sum == 0)
{
cout << "sum == 0" << endl;
}
else
{
cout << "sum != 0" << endl;
}
return 0;
}
使用memcheck如下:
4、创建与销毁不一致:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int *p = (int *)malloc(sizeof(int));
*p = 4;
cout << "*p = " << *p << endl;
delete p;
return 0;
}
使用memcheck如下:
版权声明:本文标题:valgrind进行内存检查的简单使用 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1725889577a1047400.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论