Valgrind快速使用

1.Valgrind 的核心组件(工具集)

  • memcheck 内存泄漏、内存错误检测(越界 / 野指针 / 双重释放)

  • cachegrind 缓存命中率,cpu性能分析

  • callgrind 函数调用关系、执行次数、耗时分析

  • helgrind 线程竞争、死锁检测

  • massif 堆内存使用趋势分析

其中mencheck是最常用的,也是本文讲解的

核心用法:

格式:

shell 复制代码
valgrind [通用参数] --tool=memcheck [memcheck参数] 程序名 [程序参数]

基础命令:

shell 复制代码
valgrind --tool=memcheck \
  --leak-check=full \       # 详细检测所有内存泄漏
  --show-leak-kinds=all \   # 显示所有泄漏类型(确定/间接/可能)
  --track-origins=yes \     # 定位内存越界/野指针的根源(精准但稍慢)
  --verbose \               # 输出额外调试信息
  --log-file=valgrind.log \ # 文本日志输出到文件
  --xml=yes \               # 启用 XML 格式输出
  --xml-file=valgrind.xml \ # XML 若使用xml日志输出到文件(必须补充!)
  ./test                    # 待检测的程序(可加参数,如 ./test 123

参数:

  • -- show-leak-kinds=all 显示 4 种泄漏类型:
    • definitely lost:明确泄漏(必须修复)
    • indirectly lost:间接泄漏(如容器内对象泄漏)
    • possibly lost:可能泄漏(需确认)
    • still reachable:内存未释放但可访问(如全局对象,可忽略)

几个例子快速熟悉使用:

1.new/new[] 与 delete/delete[]

new没有delete

c++ 复制代码
#include <iostream>
#include <string>
using namespace std;
void test_basic_leak() {
    int* num = new int(100);          // 分配堆内存
    std::string* str = new std::string("test leak"); // 分配堆内存
    
    {
    // 业务逻辑后直接返回,未释放指针
    /*  ....        */
        return ;
    }
    delete num; 
    delete str;
    num = nullptr ;
    str = nullptr ; 
}
int main(){
    test_basic_leak();
    return 0;
}
shell 复制代码
g++ -o test1 -g -O0 test.cc  # -g 可获取具体行号

new[] 而后 delete

c++ 复制代码
void test2(){
    int *arr = new int[10];
    /*
        ... ... 
    */
    delete arr;
}
shell 复制代码
 valgrind --leak-check=full --show-leak-kinds=all --log-file=v_2 ./test2

容器使用不当

c++ 复制代码
void test3(){
    vector<int*> arr(5) ;
    for(int i = 0 ; i < arr.size() ; ++i) {
        arr[i] = new int(i);
    } 
    arr.clear();
}
shell 复制代码
 valgrind --leak-check=full --show-leak-kinds=all --log-file=v_3 ./test3

类设计缺陷:

成员指针未释放
浅拷贝导致两次释放
c++ 复制代码
class MyClass{
public:
    MyClass(){
        buffer = new char[1024];
    }
    ~MyClass(){
        delete[] buffer;
    }
private:
    char *buffer ;
};
void test5(){
    MyClass obj1;
    MyClass obj2 = obj1 ;
} 
相关推荐
肆忆_10 小时前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星14 小时前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛2 天前
delete又未完全delete
c++
端平入洛3 天前
auto有时不auto
c++
哇哈哈20214 天前
信号量和信号
linux·c++
多恩Stone4 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
蜡笔小马4 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
超级大福宝4 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
weiabc4 天前
printf(“%lf“, ys) 和 cout << ys 输出的浮点数格式存在细微差异
数据结构·c++·算法
问好眼4 天前
《算法竞赛进阶指南》0x01 位运算-3.64位整数乘法
c++·算法·位运算·信息学奥赛