我是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)

基数
    #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

驱动程序
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);
}

哈希
    /* 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错误(“不匹配的free()/删除/删除[]”和“肯定丢失了4个块中的18个字节”)。

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

此外,free()函数不会调用对象的析构函数,而delete会执行此操作。这可以解释为什么不调用~UCSDStudent(),从而导致程序泄漏学生姓名的内存。因此,使用delete代替free()中的~HashTable()应该可以解决这两个错误。

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

关于c++ - Valgrind中的内存泄漏和错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21749092/

10-14 18:20