C++内存泄漏,瓦尔格林德消息误导?
C++ Memory Leak, Valgrind Message Misleading?
我对C++有点陌生,我可以毫无错误地运行我的代码,但是,当我通过 Valgrind 运行它时,我遇到了内存泄漏,而且我一生都无法弄清楚我在哪里泄漏!这是我的错误消息:
==22902== in use at exit: 72,728 bytes in 2 blocks
==22902== total heap usage: 4 allocs, 2 frees, 73,816 bytes allocated
==22902==
==22902== 24 bytes in 1 blocks are definitely lost in loss record 1 of 2
==22902== at 0x4C2E80F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22902== by 0x401086: Bar::Bar(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int) (Bar.cpp:10)
==22902== by 0x400F76: main (Foo_main.cpp:20)
我有一个类,Foo,它被用作Bar类的"附件"。 Bar继承自foo。它有两个私有成员,id_ 和 name_,析构函数和 use() 在头文件中声明为虚拟。
#include "Foo.h"
#include<iostream>
using std::cout;
using std::endl;
using std::string;
Foo::Foo(int id, const string& name) :
id_(id),
name_(name){}
Foo::~Foo() {}
int Foo::id() const { return id_; }
string Foo::name() const { return name_; }
void Foo::use() const {
cout << "Using Foo #" << id_ << ", " << name_ << endl;
}
Bar有两个私人成员,num_attachments_(未签名的int)和attachments_(Foo**)
#include "Bar.h"
#include <iostream>
using std::cout;
using std::endl;
Bar::Bar(int id, const std::string& name, unsigned int num_attachments) :
Foo(id, name),
attachments_(new Foo*[num_attachments]),
num_attachments_(num_attachments) {
// explicity null out each entry in the new array
for (unsigned int i=0; i<num_attachments; ++i) {
attachments_[i] = NULL;
}
}
void Bar::use() const {
cout << "Using Bar #" << id() << endl;
for (unsigned int i=0; i<num_attachments_; ++i) {
if (attachments_[i] != NULL) {
attachments_[i]->use();
}
}
}
(注意:我知道的一些不会导致泄漏的代码被注释掉了)。 我怀疑问题出在 Bar 的 use() 函数中,但我不太确定缺少什么!
最后,这是主要功能:
Foo* f = new Bar(1, "foobar", 3);
f->use();
delete f;
当然,我可以应要求上传整个程序(尽管我觉得问题可能很明显,我只是完全错过了一些东西)。任何帮助都会很棒!
你需要为 Bar 声明并实现一个显式析构函数
内存泄漏可能一定是发生的,因为您尚未释放属性attachments_
的内存,您必须在Bar
构造函数中分配该内存。
它的析构函数应该像这样实现:
Bar::~Bar() {
for (unsigned int i = 0; i < num_attachments_; i++) {
if (attachments_[i] != NULL) {
delete attachments_[i];
}
}
if(attachments_) { // Just a safeguard good practice for defensive programming. You could omit this statement. This if statement is the same as if (attachments_ != NULL)
delete [] attachments_;
}
}
此外,由于Bar
继承自Foo
,因此在Bar.h中,将析构函数声明为虚拟已经很好,这样派生类析构函数就会在基的类析构函数之前调用。由于派生类具有动态内存,因此您希望发生这种情况。所以在Bar.h中:
class Bar: public Foo {
/* ...other class members... */
public:
/* ...other class operations... */
virtual ~Bar(); // the virtual keyword here forces the program to visit Bar's (the derived class) destructor before Foo's (the base class)destructor. It is necessary, otherwise it only invokes the Foo's destructor (leaving leaked dynamic memory from Bar)
};
尝试对 Foo Bar 对象使用 std::unique_ptr。
http://en.cppreference.com/w/cpp/memory/unique_ptr
相关文章:
- 瓦尔格林德:数学函数"Conditional jump or move depends on uninitialised value(s)"
- 为什么瓦尔格林德在不释放恶意内存后没有报告任何问题?
- 瓦尔格林德的内存泄漏使用新的
- CPP 中的瓦尔格林德和记忆泄漏:"Conditional jump or move depends on uninitialised values"
- C++程序什么都不做,但瓦尔格林德显示内存分配
- 赫尔格林德和atomic_flag
- 删除堆后对瓦尔格林德错误感到困惑
- 为什么我会"Invalid read of size 8"?(瓦尔格林德)
- 瓦尔格林德报告在 =带有嵌套shared_ptrs的运算符上的读取错误
- 瓦尔格林德:注意到但未处理的国际奥克特尔;是否有必要处理以及如何找到?
- 瓦尔格林德错误 - 地址0x0不是堆叠的 malloc'd 或自由的
- 瓦尔格林德错误来自 boost::asio
- 瓦尔格林德可能丢失了,仍然可以到达并被瓦尔格林德检测到并被抑制
- 在瓦尔格林德的泄漏摘要中使用 std::ios::sync_with_stdio(fasle) 打印时获取"still reachable"
- 瓦尔格林德"不支持arch_prctl选项"
- 有没有办法只从瓦尔格林德获得泄漏摘要?
- 瓦尔格林德 + --泄漏检查 -> 不泄漏摘要
- 我找不到瓦尔格林德告诉我的记忆链接
- C++内存泄漏,瓦尔格林德消息误导?
- 瓦尔格林德中止"killed"消息