简体   繁体   English

在malloc上的Valgrind内存泄漏

[英]Valgrind Memory Leak on malloc

I am working in a C project and I have created the following hash table implementation: 我在一个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;
    }
}

The client code that uses the above implementation is the following: 使用上述实现的客户端代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "hash_table.h"

int main(int argc, char** argv)
{
    int call_type;
    int done;
    hash_table* h_table;
    hash_init(&h_table, 67);
    char* sample_key;
    int i;
    hash_record* h_table_iterator;
    unsigned int bucket_iterator;
    for(i = 0; i < 1000; i++)
    {
        sample_key = malloc(sizeof(char)*(1 + strlen("key_XXXXXXX")));
        sprintf(sample_key, "key_%d", i);
        hash_put_value(&h_table, sample_key, sample_key);
        free(sample_key);
    }

    hash_destroy(&h_table);
    return 0;
}

When I execute my code with Valgrind I get the following: 当我使用Valgrind执行代码时,得到以下信息:

db2inst1@bear:~/Documents/bigintegration/Recode-UDF$ valgrind --tool=memcheck --leak-   check=yes --show-reachable=yes --num-callers=20 ./hash_test
==3031== Memcheck, a memory error detector
==3031== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==3031== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==3031== Command: ./hash_test
==3031== 
==3031== 
==3031== HEAP SUMMARY:
==3031==     in use at exit: 37,100 bytes in 2,799 blocks
==3031==   total heap usage: 4,069 allocs, 1,270 frees, 53,404 bytes allocated
==3031== 
==3031== 7,354 bytes in 933 blocks are indirectly lost in loss record 1 of 3
==3031==    at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3031==    by 0x40173C: hash_put_value (in /home/db2inst1/Documents/bigintegration/Recode-UDF/hash_test)
==3031==    by 0x40091D: main (in /home/db2inst1/Documents/bigintegration/Recode-UDF/hash_test)
==3031== 
==3031== 7,354 bytes in 933 blocks are indirectly lost in loss record 2 of 3
==3031==    at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3031==    by 0x40178F: hash_put_value (in /home/db2inst1/Documents/bigintegration/Recode-UDF/hash_test)
==3031==    by 0x40091D: main (in /home/db2inst1/Documents/bigintegration/Recode-UDF/hash_test)
==3031== 
==3031== 37,100 (22,392 direct, 14,708 indirect) bytes in 933 blocks are definitely lost in loss record 3 of 3
==3031==    at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64- linux.so)
==3031==    by 0x401705: hash_put_value (in /home/db2inst1/Documents/bigintegration/Recode-UDF/hash_test)
==3031==    by 0x40091D: main (in /home/db2inst1/Documents/bigintegration/Recode-UDF/hash_test)
==3031== 
==3031== LEAK SUMMARY:
==3031==    definitely lost: 22,392 bytes in 933 blocks
==3031==    indirectly lost: 14,708 bytes in 1,866 blocks
==3031==      possibly lost: 0 bytes in 0 blocks
==3031==    still reachable: 0 bytes in 0 blocks
==3031==         suppressed: 0 bytes in 0 blocks
==3031== 
==3031== For counts of detected and suppressed errors, rerun with: -v
==3031== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)

I do not understand what is the cause of the memory leaks caught by Valgrind. 我不明白Valgrind捕获内存泄漏的原因是什么。 I am really concerned about this matter because I usually allocate memory like this and I believe that since Valgrind complains, I must be doing something wrong. 我真的很担心这件事,因为我通常会这样分配内存,并且我相信由于Valgrind抱怨,所以我一定做错了事。

I browsed other posts about this matter, but I have not found something similar to my code. 我浏览了有关此问题的其他文章,但没有找到与我的代码相似的内容。 Any thoughts, advices, indications on what I am doing wrong? 关于我在做什么错的任何想法,建议和指示吗?

Thank you, Nick 谢谢尼克

In hash_put_value hash_put_value

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

should be 应该

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

Your current code leaks the existing head_record . 您当前的代码泄漏了现有的head_record

If you wanted to simplify your code you could now shorten 如果您想简化代码,现在可以缩短

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;
}

to

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

There is another potential leak in your code. 您的代码中还有另一个潜在的泄漏。 hash_init would create an entry for zero-length key or value but hash_destroy will skip freeing them. hash_init将为零长度的键或值创建一个条目,但hash_destroy将跳过释放它们的操作。 It is safe to call free on a NULL pointer or (allocated) empty string so 在NULL指针或(分配的)空字符串上调用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);
}

should be 应该

free(tmp_rec_2->value);
free(tmp_rec_2->key);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM