Valgrind泄漏不确定它在哪里?

时间:2014-06-30 19:32:52

标签: c malloc valgrind free

不确定未释放16个字节的确切位置。任何想到最后一次免费的地方都会很棒。我对C和编程也很陌生。

==23862== HEAP SUMMARY:
==23862==     in use at exit: 16 bytes in 1 blocks
==23862==   total heap usage: 25 allocs, 24 frees, 2,146 bytes allocated
==23862== 
==23862== 16 bytes in 1 blocks are definitely lost in loss record 1 of 1
==23862==    at 0x4A069EE: malloc (vg_replace_malloc.c:270)
==23862==    by 0x400B5D: read_from_file (sorting.c:145)
==23862==    by 0x40093F: main (sorting.c:73)
==23862==
==23862== LEAK SUMMARY:
==23862==    definitely lost: 16 bytes in 1 blocks
==23862==    indirectly lost: 0 bytes in 0 blocks
==23862==      possibly lost: 0 bytes in 0 blocks
==23862==    still reachable: 0 bytes in 0 blocks
==23862==         suppressed: 0 bytes in 0 blocks

然而,如果我取出第146行,一切都被释放,我从4个上下文得到四个错误,说条件跳转或移动取决于未初始化的值。

==31575== Conditional jump or move depends on uninitialised value(s)
==31575==    at 0x400E31: length (sorting.c:238)
==31575==    by 0x400C7D: bubble_sort (sorting.c:186)
==31575==    by 0x4009B8: main (sorting.c:80)
==31575== 
==31575== Conditional jump or move depends on uninitialised value(s)
==31575==    at 0x400DF3: display (sorting.c:225)
==31575==    by 0x4009FC: main (sorting.c:83)
==31575== 
==31575== Conditional jump or move depends on uninitialised value(s)
==31575==    at 0x4A063A3: free (vg_replace_malloc.c:446)
==31575==    by 0x400B2F: destroy (sorting.c:138)
==31575==    by 0x400A08: main (sorting.c:85)
==31575== 
==31575== Conditional jump or move depends on uninitialised value(s)
==31575==    at 0x400B41: destroy (sorting.c:134)
==31575==    by 0x400A08: main (sorting.c:85)

我的代码是:

typedef struct Data_ {

    char *name;
    struct Data_ *nextData;

} Data;

int main(int argc, char **argv)
{
    Data *head = NULL;
    const int size = atoi(argv[2]);
    head = read_from_file(argv[1], size); //line 73
    head = bubble_sort(head);
    destroy(head);
    head = NULL;
    return 0;
}

Data* read_from_file(const char *file, const int size)
{
    Data *head = malloc(sizeof(Data)); //line 145
    head = NULL; //line 146

    FILE* in;
    in = fopen(file,"r");

    if(file == NULL)
    {
        printf("Unable to open %s\n",file);
        exit(1);
    }

    char name[MAX_STR_LEN];
    int i = 0;
    for(;i<size;i++)
    {
        fscanf(in,"%s", name);
        push(&head,name);
    }
    fclose(in);
    return head;
}

void destroy(Data* list)
{
    Data *current = list;
    Data *needs_freeing;

    while(current)
    {
        needs_freeing = current;
        current = current->nextData;
        free(needs_freeing->name);
        free(needs_freeing);
    }
}

void push(Data **head, char *name)
{
    Data *new = malloc(sizeof(Data));
    new->name = malloc(sizeof(char)*MAX_STR_LEN);

    if(new)
    {
        strcpy(new->name, name);
        new->nextData = *head;
        *head = new;
    }
}

Data* bubble_sort(Data *list)
{
    Data *current;
    Data *previous;
    char temp[MAX_STR_LEN];

    int list_length = length(list);
    int i, j;
    for(i=1;i<list_length;i++)
    {
        previous = list;
        current = previous->nextData;
        for(j=0;j<list_length-2;j++)
        {
            if(strcmp(previous->name, current->name) > 0)
            {
                previous = swap(previous, current);
            }
            previous = previous->nextData;
            current = current->nextData;
        }
    }
    return list;
}

Data* swap(Data *left, Data *right)
{
    char temp[MAX_STR_LEN];

    strcpy(temp, left->name);
    strcpy(left->name, right->name);
    strcpy(right->name, temp);
    return left;
}

void display(Data *list)
{
    FILE* file;
    file = fopen("out.txt", "w");
    if(file == NULL)
    {
        printf("Unable to write to file\n");
        exit(1);
    }
    while(list->nextData != NULL)
    {
        fprintf(file,"%s\n",list->name);
        list = list->nextData;
    }
    fclose(file);
}


int length(Data* list)
{
    int count = 0;
    Data* current = list;
    while(current)
    {
        current = current->nextData;
        ++count;
    }
    return count;
}

1 个答案:

答案 0 :(得分:2)

Data *head = malloc(sizeof(Data)); //line 145
head = NULL; //line 146

这是内存泄漏。您为大小为sizeof (Data)的对象分配内存,但随后覆盖指针。此外,如果您打算在*head中进行读取或写入,则不使用已分配的对象就无法使指针无效。