Valgrind内存泄漏和错误

memory leaks and errors in Valgrind

本文关键字:错误 泄漏 内存 Valgrind      更新时间:2023-10-16

我是C++的初学者,如果我正确地释放了内存并删除了可能的悬空指针,我仍然非常困惑。这是我过去学校的一项作业。有那么多学生有同样的问题,没有其他人能帮助我。请确定我的问题所在。

==25334== Mismatched free() / delete / delete []
==25334==    at 0x4006D21: free (vg_replace_malloc.c:446)
==25334==    by 0x80492F2: HashTable::~HashTable() (Hash.c:115)
==25334==    by 0x8049145: SymTab::~SymTab() (SymTab.h:9)
==25334==    by 0x8048E9D: main (Driver.c:170)
==25334==  Address 0x402c0b8 is 0 bytes inside a block of size 12 alloc'd
==25334==    at 0x4007862: operator new(unsigned int) (vg_replace_malloc.c:292)
==25334==    by 0x8048C73: main (Driver.c:143)
==25334==
==25334==
==25334== HEAP SUMMARY:
==25334==     in use at exit: 18 bytes in 4 blocks
==25334==   total heap usage: 10 allocs, 6 frees, 106 bytes allocated
==25334==
==25334== 18 bytes in 4 blocks are definitely lost in loss record 1 of 1
==25334==    at 0x4007D58: malloc (vg_replace_malloc.c:270)
==25334==    by 0x97E96F: strdup (strdup.c:43)
==25334==    by 0x8048FDC: UCSDStudent::UCSDStudent(char*, long) (Driver.c:36)
==25334==    by 0x8048C92: main (Driver.c:143)
==25334==
==25334== LEAK SUMMARY:
==25334==    definitely lost: 18 bytes in 4 blocks
==25334==    indirectly lost: 0 bytes in 0 blocks
==25334==      possibly lost: 0 bytes in 0 blocks
==25334==    still reachable: 0 bytes in 0 blocks
==25334==         suppressed: 0 bytes in 0 blocks
==25334==
==25334== For counts of detected and suppressed errors, rerun with: -v
==25334== ERROR SUMMARY: 5 errors from 2 contexts (suppressed: 15 from 8)

Base.h

#ifndef BASE_H
#define BASE_H
#include <iostream>
using namespace std; /* C error */
/* TEMPLATE */
struct Base { /* C++ struct is public class, public methods */
/* PUBLIC SECTION */
/* virtual: candidates for redefinition */
virtual operator char * (void) { 
return 0;
}
virtual operator long (void) {      // hash function
return 0;
}
virtual long operator == (Base & base) {// isequal function
return *this == base;
}
Base (void) {}              // new_element
virtual ~Base (void) {}         // delete_element
virtual ostream & Write (ostream & stream) = 0;// write_element
};
#endif

Driver.c

class UCSDStudent : public Base { /* extends Base  */
char * name;
long studentnum;
public:
UCSDStudent (char * nm, long sn) :
name (strdup (nm)), studentnum (sn) {} /* Initialization */

~UCSDStudent (void) { /* Destructor */
free (name);
}

Hash.c

/* HashTable constructor */
HashTable :: HashTable (int sz) : size (sz),
table_count(++counter), occupancy (0), table (new Base *[sz]), 
probeCount (new int[sz])

HashTable :: ~HashTable (void)
{
/* call function to delete individual elements */
for(int index2 = 0; index2 < size; index2++)
{
if(table[index2] != NULL)
{
free(table[index2]);
table[index2] = NULL;
}
delete table[index2];
}
/*
* delete table itself
* Freed memory
*/
delete[] table;
delete[] probeCount;
/* pointed dangling ptr to NULL */
table = NULL; 
probeCount = NULL; 
} /* end: ~HashTable */

Valgrind的两个错误("Mismatched free()/delete/delete[]"answers"4个块中的18个字节肯定丢失了")可能是相关的。

~HashTable()中,您调用free(table[index2]),这可能意味着销毁UCSDStudent对象(不确定,因为您没有发布整个程序,尤其是没有发布将元素插入HashTable的代码)。我假设您使用new-创建UCSDStudent对象,在这种情况下,您还必须使用相应的销毁方法(在本例中为delete而不是free())。这是第一个Valgrind错误的原因。

此外,free()函数不会调用对象的析构函数,而delete会调用。这可以解释为什么~UCSDStudent()没有被调用,导致您的程序泄漏学生名称的内存。因此,在~HashTable()中使用delete而不是free()应该可以解决这两个错误。

通常,您应该尝试使用一种内存分配方式(malloc()/free()new/new[]/delete/delete[])。考虑到这是一个C++程序,new将是合适的选择。同样,我建议您删除strdup()char*的内容,改为std::string——这将删除另一个可能混淆free()delete的位置。

您在内存上调用free,该内存似乎已使用new声明,这是Valgrind在内存中出现的主要错误。您似乎也没有遵循"三条规则"(尽管这似乎不是您的全部代码)。

我强烈建议您改用智能指针,如std::shared_ptr / std::unique_ptr,并使用std::vector / std::array来创建容器。

在我看来,你从来没有打过~UCSDStudent。不幸的是,从你发布的代码中无法判断,但析构函数本身看起来不错,所以我认为问题是没有调用析构函数。