无法找到Valgrind检测到的内存泄漏问题

3

我正在使用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中实现Set的方法:

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个回答

3

你没有泄漏任何东西-只是误解了valgrind告诉你的内容。

它认为在_dl_init()下面可能存在一些问题,但这是一个转移话题。您可以安全地将其添加到valgrind抑制文件中(这总是一个好习惯,这样您就不会受到系统库的错误警报的困扰)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接