找不到Valgrind检测到的内存泄漏

时间:2016-10-05 02:50:00

标签: c++ memory-leaks valgrind

我使用 Valgrind 运行和分析此代码:

int main() {
    Set<int> c;
    return 0;
 }

所以输出是:

jscherman@jscherman:~/ClionProjects/algo2-t3-bts$ g++ set.hpp tests.cpp && valgrind --leak-check=yes --show-leak-kinds=all ./a.out
==3528== Memcheck, a memory error detector
==3528== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==3528== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==3528== Command: ./a.out
==3528== 
test_mleak...ok
==3528== 
==3528== HEAP SUMMARY:
==3528==     in use at exit: 72,704 bytes in 1 blocks
==3528==   total heap usage: 2 allocs, 1 frees, 73,728 bytes allocated
==3528== 
==3528== 72,704 bytes in 1 blocks are still reachable in loss record 1 of 1
==3528==    at 0x4C2DC10: malloc (vg_replace_malloc.c:299)
==3528==    by 0x4EC3EFF: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==3528==    by 0x40104E9: call_init.part.0 (dl-init.c:72)
==3528==    by 0x40105FA: call_init (dl-init.c:30)
==3528==    by 0x40105FA: _dl_init (dl-init.c:120)
==3528==    by 0x4000CF9: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==3528== 
==3528== LEAK SUMMARY:
==3528==    definitely lost: 0 bytes in 0 blocks
==3528==    indirectly lost: 0 bytes in 0 blocks
==3528==      possibly lost: 0 bytes in 0 blocks
==3528==    still reachable: 72,704 bytes in 1 blocks
==3528==         suppressed: 0 bytes in 0 blocks
==3528== 
==3528== For counts of detected and suppressed errors, rerun with: -v
==3528== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

显然,我在Set的构造函数中失去了记忆,但我无法找到真正的原因。这就是我实现设置(在 BTS 中)的方式:

template<class T>
class Set {
public:
    Set() : root_(NULL), cardinal_(0) {}
    ~Set() {delete root_;}
    void insert(const T &);
    bool belongs(const T &) const;
    void remove(const T &);
    const T &min() const;
    const T &max() const;
    unsigned int cardinal() const;

private:

    struct Node {
        Node(const T &v) : value(v), left(NULL), right(NULL) {}
        ~Node() {delete right; delete left;}
        T value;
        Node *left;
        Node *right;
    };

    Node *root_;
    int cardinal_;
}

知道如何解决这个漏洞吗?谢谢!

1 个答案:

答案 0 :(得分:3)

你没有泄露任何东西 - 你只是误解了valgrind告诉你的事情。

它认为_dl_init()下面可能存在一些问题,但这是一个红色的鲱鱼。你可以安全地将它添加到你的valgrind抑制文件中(这总是一件好事,所以你不会受到来自系统库的错误警报的困扰。)

相关问题