Valgrind内存泄漏和错误
memory leaks and errors in Valgrind
我是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
。不幸的是,从你发布的代码中无法判断,但析构函数本身看起来不错,所以我认为问题是没有调用析构函数。
- HBOEHM 垃圾回收器未初始化的值错误和泄漏
- 修复了瓦尔格林德错误内存泄漏的错误
- Valgrind 错误和内存泄漏与 Python/C API
- 行为:内存操作错误或内存泄漏C++
- 将 SharedPtr 与 std::list 一起使用时内存泄漏!错误
- 为什么静态类成员没有内存泄漏错误?
- C 内存泄漏错误在实现堆栈类时
- C 机器人,作业分配 - 由于内存泄漏而导致阀门错误.确实需要一些提示
- Valgrind内存泄漏和错误
- Valgrind不会报告现有的泄漏/错误的条件
- 从函数返回错误定位的指针,是否会导致内存泄漏
- purify中存在错误的内存泄漏
- C++帮助-从链表中泄漏数据和分段错误
- C++内存泄漏检查中的瓦尔格林德错误
- 隔离错误和内存泄漏
- 如何使 cuda-memcheck 在出现泄漏检查错误时返回非零
- Valgrind 检测到库中的内存泄漏和错误
- 我的 MFC 应用中报告了错误的内存泄漏
- opencv函数transpose()给出错误结果,内存泄漏
- Valgrind内存泄漏错误的文件跟踪