gpt4 book ai didi

c++ - 无法找到 Valgrind 检测到的内存泄漏

转载 作者:太空狗 更新时间:2023-10-29 21:14:47 26 4
gpt4 key购买 nike

我正在使用 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 的构造函数中丢失了内存,但我找不到真正的原因。这就是我实现 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_;
}

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

最佳答案

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

它认为 _dl_init() 下可能存在一些问题,但这是一个转移注意力的问题。您可以安全地将它添加到您的 valgrind 抑制文件中(拥有它总是一件好事,这样您就不会被系统库的错误警报所困扰)。

关于c++ - 无法找到 Valgrind 检测到的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39864678/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com