c++ - 自定义链表 C++ 中的内存泄漏

标签 c++ list memory-leaks valgrind

对于我不能使用 STL 列表的作业,它必须是自定义列表。正如标题所述,即使我在节点\项目上调用 delete,我也有内存泄漏。我将不胜感激。

列表来源

template <typename T>
class DLinkList
{
private:
    struct Node
    {
        T data;
        Node *nextNode;
        Node *prevNode;
        Node(T data, Node *nextNode = nullptr, Node *prevNode = nullptr)
        {
            this->data = data;
            this->nextNode = nextNode;
            this->prevNode = prevNode;
        }
        ~Node() { delete data; }
    };

    Node *head;
    Node *tail;

public:
    DLinkList();
    ~DLinkList();

    void push_back(T data);   
};

template <typename T>
inline void DLinkList<T>::push_back(T data)
{
    if (isEmpty())
    {
        head = new Node(data);
        tail = head;
    }
    else
    {
        tail->nextNode = new Node(data, nullptr, tail);
        tail = tail->nextNode;
    }
}

template <typename T>
DLinkList<T>::DLinkList()
{
    head = nullptr;
    tail = nullptr;
}

template <typename T>
DLinkList<T>::~DLinkList()
{
    Node *ptr = head;
    while (ptr->nextNode != nullptr)
    {
        Node *garbage = ptr;
        ptr = ptr->nextNode;
        delete garbage;
    }
}

Foo 类和 main

class Foo
{
public:
    Foo() { i = 0; d = 0.0; }
    Foo(int i, double d) { this->i = i; this->d = d; }

    int getInteger() { return i; }
    double getDouble() { return d; }

private:
    int i;
    double d;
};


int main()
{
    DLinkList<Foo*> f1;
    f1.push_back(new Foo());
    f1.push_back(new Foo(2, 5.5));

    cout << "1st Values: " << f1.at(0)->getInteger() << ", " << f1.at(0)->getDouble() << endl;
    cout << "2nd Values: " << f1.at(1)->getInteger() << ", " << f1.at(1)->getDouble() << endl;

    return 0;
}

来自 valgrind

==12125== 40 (24 direct, 16 indirect) bytes in 1 blocks are definitely lost in loss record 3 of 3
==12125==    at 0x4C29203: operator new(unsigned long) (vg_replace_malloc.c:334)
==12125==    by 0x400FD8: DLinkList<Foo*>::push_back(Foo*) (DLinkList.hpp:138)
==12125==    by 0x400CF3: main (Source.cpp:28)

我不确定这里的内存是怎么丢失的,我想说是因为它正在复制它而原来的丢失了。如果是这种情况,我不知道如何处理。

再次感谢任何有助于理解这一点的帮助。我试图查看所有相关问题,但我没有看到任何涵盖此内容的内容,或者至少我不理解。谢谢!

最佳答案

考虑到评论中指出的其他问题,例如错误使用显式调用 DLinkList 析构函数,您在 main() 中这样做:

f1.push_back(new Foo());
f1.push_back(new Foo(2, 5.5));

你在这两行无法恢复的代码上造成了即时内存泄漏。您正在调用 new,但您没有保存从 new 返回的地址,以便稍后可以为该地址调用 delete

因此 main 必须管理这些动态分配的 Foo 对象。像这样:

Foo* p1 = new Foo();
Foo *p2 = new Foo(2, 5.5);
f1.push_back(p1);
f1.push_back(p2);
//...
delete p1;
delete p2;

这应该可以解决内存泄漏问题,但在当今的 C++ 时代,这是糟糕的编程习惯。您很可能会使用

DLinkList<Foo>

并将Foo对象放在链表中,因此不需要在main中进行任何手动内存管理,或者使用智能指针并拥有智能指针的链表,即

DLinkList<std::unique_ptr<Foo>>

关于c++ - 自定义链表 C++ 中的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47245474/

相关文章:

c# - 使用 LINQ 的两个列表的交集

c++ - VS 2017 C++ 编译器在 VS 2005 中找不到匹配的函数

"Empty classes"的 C++ 多重继承内存布局

c# - 此 List<List> 中有什么不正确的地方?

c++ - VmRSS 正在增长

node.js - EventEmitter 内存泄漏。需要 ('request' )

windows - 为什么系统在使用指针 Windows 时分配更多内存?

c++ - 通过小部件获取顶级窗口

c++ - Q学习飞行棋游戏?

python-3.x - 如何以 float 为步长迭代整数列表?