malloc上的Valgrind内存泄漏

malloc上的Valgrind内存泄漏,c,valgrind,C,Valgrind,我在一个C项目中工作,我创建了以下哈希表实现: typedef struct hash_record { char* key; char* value; struct hash_record* next; }hash_record; typedef struct hash_bucket { char* key; hash_record* head_record; }hash_bucket; typedef struct hash_table {

我在一个C项目中工作,我创建了以下哈希表实现:

typedef struct hash_record 
{
    char* key;
    char* value;
    struct hash_record* next;
}hash_record;

typedef struct hash_bucket
{
    char* key;
    hash_record* head_record;
}hash_bucket;

typedef struct hash_table 
{
    int bucket_num;
    hash_bucket** hash_entry;
}hash_table;

int hash_init(hash_table** h_table, int bucket_num)
{
    int i;
    (*h_table) = malloc(sizeof(char)*sizeof(hash_table));

    assert((*h_table) != NULL);
    (*h_table)->hash_entry = malloc(sizeof(hash_bucket*) * bucket_num);
    assert((*h_table)->hash_entry != NULL);
    (*h_table)->bucket_num = bucket_num;
    for(i = 0; i < bucket_num; i++)
    {
        (*h_table)->hash_entry[i] = malloc(sizeof(hash_bucket));
        (*h_table)->hash_entry[i]->head_record = NULL;
        (*h_table)->hash_entry[i]->key = NULL;
    }
    return 0;
}

int hash_destroy(hash_table** h_table)
{
    int i;
    hash_record* tmp_rec, *tmp_rec_2;

    if((*h_table) == NULL)
    {
        return 0;
    }
    for(i = 0; i < (*h_table)->bucket_num; i++)
    {
        assert((*h_table)->hash_entry[i] != NULL);
        tmp_rec = (*h_table)->hash_entry[i]->head_record;
        while(tmp_rec != NULL)
        {
            assert((tmp_rec != NULL) && (tmp_rec->value != NULL));
            tmp_rec_2 = tmp_rec;
            tmp_rec = tmp_rec->next;
            if(tmp_rec_2->value != NULL && strlen(tmp_rec_2->value) > 0 && 
                tmp_rec_2->key != NULL && strlen(tmp_rec_2->key) > 0)
            {
                free(tmp_rec_2->value);
                free(tmp_rec_2->key);
            }
            free(tmp_rec_2);
        }
        assert((*h_table)->hash_entry[i] != NULL);
        free((*h_table)->hash_entry[i]);
    }
    free((*h_table)->hash_entry);
    free((*h_table));
    return 0;
}

void hash_put_value(hash_table** h_table, char* key, char* value)
{
    hash_record* tmp_rec, *tmp_rec2;

    assert((*h_table) != NULL);
    assert((*h_table)->bucket_num > 0);
    assert((*h_table)->hash_entry != 0);
    assert(key !=  NULL && strlen((char*) key) > 0);
    assert(value != NULL);
    assert(strlen(value) > 0);

    unsigned int bucket_no = FNVHash (key, strlen(key));
    bucket_no = bucket_no % (*h_table)->bucket_num;

    assert((*h_table)->hash_entry[bucket_no] != NULL);

    tmp_rec = malloc(sizeof(hash_record));
    tmp_rec->key = malloc(sizeof(char)*(1 + strlen(key)));
    strcpy(tmp_rec->key, key);
    tmp_rec->value = malloc(sizeof(char)*(1 + strlen(value)));
    strcpy(tmp_rec->value, value);
    tmp_rec->next = NULL;

    if( (*h_table)->hash_entry[bucket_no]->head_record == NULL )
    {
        (*h_table)->hash_entry[bucket_no]->head_record = tmp_rec;
    }
    else
    {
        tmp_rec->next = (*h_table)->hash_entry[bucket_no]->head_record->next;
        (*h_table)->hash_entry[bucket_no]->head_record = tmp_rec;
    }
}
我不明白Valgrind发现内存泄漏的原因是什么。我真的很担心这件事,因为我通常是这样分配内存的,我相信既然Valgrind抱怨,我一定是做错了什么

我浏览了关于这件事的其他帖子,但我没有发现类似于我的代码的东西。关于我做错了什么,有什么想法、建议、指示吗

谢谢,,
尼克

哈希值中

tmp_rec->next = (*h_table)->hash_entry[bucket_no]->head_record->next;
应该是

tmp_rec->next = (*h_table)->hash_entry[bucket_no]->head_record;
free(tmp_rec_2->value);
free(tmp_rec_2->key);
您当前的代码泄漏了现有的
头记录

如果您想简化代码,现在可以缩短代码

tmp_rec->next = NULL;
if( (*h_table)->hash_entry[bucket_no]->head_record == NULL )
{
    (*h_table)->hash_entry[bucket_no]->head_record = tmp_rec;
}
else
{
    tmp_rec->next = (*h_table)->hash_entry[bucket_no]->head_record;
    (*h_table)->hash_entry[bucket_no]->head_record = tmp_rec;
}

您的代码中还有另一个潜在的漏洞
hash_init
将为零长度键或值创建一个条目,但
hash_destroy
将跳过释放它们。在空指针或(分配的)空字符串上调用
free
是安全的

if(tmp_rec_2->value != NULL && strlen(tmp_rec_2->value) > 0 && 
    tmp_rec_2->key != NULL && strlen(tmp_rec_2->key) > 0)
{
    free(tmp_rec_2->value);
    free(tmp_rec_2->key);
}
应该是

tmp_rec->next = (*h_table)->hash_entry[bucket_no]->head_record;
free(tmp_rec_2->value);
free(tmp_rec_2->key);

Valgrind似乎认为这是在哈希值中发生的。也许您应该使用调试标志进行编译以获得更多信息(关于函数中的哪个malloc)。谢谢您的回答。对我有用的信息。但是,我想问您关于释放空指针的问题。这不是会引起问题吗?C标准保证了
free(NULL)
的行为。e、 g.C11 7.22.3.3说“自由函数导致ptr指向的空间被释放,即可用于进一步分配。如果ptr是空指针,则不会发生任何操作…”谢谢。我不知道这个细节。它至少可以保证回到ANSI 1989标准。“我没有挖得更深了。”约翰·耶普说。我选择C11是因为人们倾向于选择参考文献而不是最新的标准。我并不是说这种行为是针对较新版本的C语言的。所有符合标准的编译器都会支持它。
free(tmp_rec_2->value);
free(tmp_rec_2->key);