在另一个类中执行删除时出现Valgrind错误 我尝试在对象位置低时测试C++性能,因此我尝试分配大量内存,其中有许多“死区对象”。当“活对象”之间有许多“死对象”时,我将对它们进行基准测试

在另一个类中执行删除时出现Valgrind错误 我尝试在对象位置低时测试C++性能,因此我尝试分配大量内存,其中有许多“死区对象”。当“活对象”之间有许多“死对象”时,我将对它们进行基准测试,c++,memory-leaks,valgrind,new-operator,delete-operator,C++,Memory Leaks,Valgrind,New Operator,Delete Operator,为此,我定义了一个简单的LinkedList: #include "LinkedList.hpp" #include <iostream> #include <string> LinkedList::LinkedList() { this->first = NULL; this->last = NULL; this->size = 0; } void LinkedList::add(node_t *node) { if

为此,我定义了一个简单的LinkedList:

#include "LinkedList.hpp"
#include <iostream>
#include <string>

LinkedList::LinkedList() {
    this->first = NULL;
    this->last = NULL;
    this->size = 0;
}

void LinkedList::add(node_t *node) {
    if (!last) {
        first = node;
        last = first;
        size++;
        return;
    }
    last->next = node;
    last = last->next;
    size++;
}


void LinkedList::deleteFirst() {
    if (first == NULL || size <= 0) {
        std::cout << "Cannot Delete from empty list" << std::endl;
        return;
    }
    node_t* oldfirst = first;
    first = first->next;
    delete oldfirst;
    size--;
}
在尝试实验时,我注意到valgrind显示我有一些内存泄漏。我很确定我正在删除每个分配的对象。以下是我的主要观点:

#include "LinkedList.hpp"
#include <ctime>
#include <cstdlib>

bool doConnect() {
    int  r;
    r = rand();

    return ((r % 2) == 1);
}

int main() {

    srand(time(NULL));
    int size = 100000;
    int i = 0;
    LinkedList *node_list = new LinkedList();
    LinkedList *dead_node_list = new LinkedList();


    for (i=0; i < size; i++) {
        node_t *new_node = new node_t();
        if (doConnect()) {
            node_list->add(new_node);
        }
        else {
            dead_node_list->add(new_node);
        }
    }


    for (i=0; i < dead_node_list->getSize(); i++)
            dead_node_list->deleteFirst();

    for (i=0; i < node_list->getSize(); i++)
            node_list->deleteFirst();

    delete node_list;
    delete dead_node_list;



    return 0;
}
我错过了什么明显的东西吗


我使用:g++-Wall-g LinkedList.cpp main.cpp-o main编译代码

您认为变量
I
在这段代码中做了什么

for (i=0; i < dead_node_list->getSize(); i++)
        dead_node_list->deleteFirst();

与其他列表类似。

不一定是您“泄漏”了内存,可能是标准运行时系统的某个部分执行了程序其余部分所需的一次性分配,从而导致误报(因为进程退出时操作系统会释放内存)。您是否尝试按照Valgrind给出的建议添加了
--leak check=full
选项?是(忘记提及),相同的输出。泄漏应该发生在main的第22行(我通过for循环内部的new分配内存的那一行),这是一个多么愚蠢的错误。。不知道为什么我想不出来。这很明显。谢谢
==15291== Memcheck, a memory error detector
==15291== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==15291== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==15291== Command: ./main
==15291== 
==15291== 
==15291== HEAP SUMMARY:
==15291==     in use at exit: 199,720 bytes in 24,965 blocks
==15291==   total heap usage: 100,002 allocs, 75,037 frees, 800,048 bytes allocated
==15291== 
==15291== LEAK SUMMARY:
==15291==    definitely lost: 16 bytes in 2 blocks
==15291==    indirectly lost: 199,704 bytes in 24,963 blocks
==15291==      possibly lost: 0 bytes in 0 blocks
==15291==    still reachable: 0 bytes in 0 blocks
==15291==         suppressed: 0 bytes in 0 blocks
==15291== Rerun with --leak-check=full to see details of leaked memory
==15291== 
==15291== For counts of detected and suppressed errors, rerun with: -v
==15291== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
for (i=0; i < dead_node_list->getSize(); i++)
        dead_node_list->deleteFirst();
while ( dead_node_list->getSize())
        dead_node_list->deleteFirst();