C++;内存泄漏,Valgrind消息误导? 我对C++有点新,我可以运行我的代码而不出错,但是,当我通过ValGRND运行它时,我会遇到内存泄漏,我不能为我的生命找出我在哪里泄漏!以下是我的错误消息: ==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)

C++;内存泄漏,Valgrind消息误导? 我对C++有点新,我可以运行我的代码而不出错,但是,当我通过ValGRND运行它时,我会遇到内存泄漏,我不能为我的生命找出我在哪里泄漏!以下是我的错误消息: ==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),c++,valgrind,C++,Valgrind,当然,我可以根据请求上传整个程序(尽管我觉得问题很明显,我只是完全遗漏了一些东西)。任何帮助都会很棒 您需要为Bar声明并实现一个显式析构函数 内存泄漏可能一定是发生了,因为您尚未释放属性附件的内存,必须在条构造函数中分配该内存 It析构函数的实现应与以下类似: Bar::~Bar() { for (unsigned int i = 0; i < num_attachments_; i++) { if (attachments_[i] != NULL) {

当然,我可以根据请求上传整个程序(尽管我觉得问题很明显,我只是完全遗漏了一些东西)。任何帮助都会很棒

您需要为Bar声明并实现一个显式析构函数 内存泄漏可能一定是发生了,因为您尚未释放属性
附件的内存,必须在
构造函数中分配该内存

It析构函数的实现应与以下类似:

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_;
    }
}

尝试对Foo-Bar对象使用std::unique_ptr


您缺少一个析构函数,它
删除
s
新建
ed数组。相当基本。很好,你声明你的析构函数是虚拟的!哇,真不敢相信我错过了,太谢谢你了!我不会再出现泄漏,但是,在一个更复杂的问题中,在for循环之后添加“delete[]attachments_u2;”是否更合适,因为它的类型是Foo**?您已经找到了。我匆匆忙忙,没赶上。我现在已经编辑了我的答案。顺便说一句:没有理由在删除之前检查NULL(请参见和)@Frunsi plus1,如果您真的知道自己在做什么,这是正确的。然而,这是一个很好的实践。应用它并不有害,尤其是在用C++内存管理启动时。
#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();
        }
    }
}
Foo* f = new Bar(1, "foobar", 3);
f->use();
delete f;
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_;
    }
}
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)
};