安装
1. yum安装
yum -y install valgrind
2. 手动安装
- 下载
- 解压
tar Valgrind压缩包 - 配置
./configure --prefix=/usr/local/valgrind - 编译
make - 安装
make install - 设置到环境变量
- 编辑个人配置文件
vim ~/.bashrc
- 增加valgrind路径到环境变量
PATH=${PATH}:/usr/local/valgrind/bin
- 更新个人配置文件
source ~/.bashrc
检测内存泄露的方式
gcc XXX.c -g -o 程序
valgrind --tool=memcheck --leak-check=full ./程序
选项--tool
| tool | 作用 |
|---|---|
| Memcheck | 发现开发中绝大多数内存错误使用情况,是选项默认值。 |
| Callgrind | 检查程序中函数调用过程中出现的问题。 |
| Cachegrind | 检查程序中缓存使用出现的问题。 |
| Helgrind | 检查多线程程序中出现的竞争问题。 |
| Massif | 检查程序中堆栈使用中出现的问题。 |
| Extension | 利用core提供的功能,编写特定的内存调试工具。 |
选项--leak-check
| leak-check | 作用 |
|---|---|
summary |
总结式内存泄露信息(默认) |
full |
详细式内存泄露信息 |
no |
不显示内存泄露信息 |
常见错误
| 错误 | 中文解释 |
|---|---|
| Use of uninitialised memory | 使用未初始化的内存 |
| Reading/writing memory after it has been free’d | 使用已经释放了的内存 |
| Reading/writing off the end of malloc’d blocks | 使用超过malloc分配的内存空间 |
| Reading/writing inappropriate areas on the stack | 对堆栈的非法访问 |
| Memory leaks – where pointers to malloc’d blocks are lost forever | 申请的空间是否有释放 |
| Mismatched use of malloc/new/new [] vs free/delete/delete [] | malloc/free/new/delete申请和释放内存的匹配 |
| Overlapping src and dst pointers in memcpy() and related functions | src和dst的重叠 |
