在closedir()之后无效的free()

时间:2016-04-01 22:45:13

标签: c linux memory-management memory-leaks

我实现了一个名为myls的ls的简单版本,当我尝试释放程序中创建的链表时,我得到了无效的free()通知,我认为它与closedir()有关但我不是&# 39;确切知道原因,这里是内存泄漏检查 :( ***表示个人信息)

******:~/***/***/myls$ valgrind --leak-check=full --show-reachable=yes myls
==742== Memcheck, a memory error detector
==742== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==742== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==742== Command: myls
==742== 
.
..
Makefile
myls
myls.c
tests
==742== Invalid free() / delete / delete[] / realloc()
==742==    at 0x4C2A82E: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==742==    by 0x4009D2: freeList (in /p2/hh/***/***/***/myls/myls)
==742==    by 0x400AE2: main (in /p2/hh/***/***/***/myls/myls)
==742==  Address 0x51f0083 is 67 bytes inside a block of size 32,816 alloc'd
==742==    at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==742==    by 0x4EECD5A: __alloc_dir (opendir.c:202)
==742==    by 0x400A0B: main (in /p2/hh/***/***/***/myls/myls)
==742== 
==742== 
==742== HEAP SUMMARY:
==742==     in use at exit: 0 bytes in 0 blocks
==742==   total heap usage: 7 allocs, 13 frees, 32,912 bytes allocated
==742== 
==742== All heap blocks were freed -- no leaks are possible
==742== 
==742== For counts of detected and suppressed errors, rerun with: -v
==742== ERROR SUMMARY: 6 errors from 1 contexts (suppressed: 2 from 2)

,这是我的代码

#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
typedef struct n{
    char *key;
    struct n *next;
} node;
node *list = NULL;

// errorCheck(*myDir) -- check if the target directory is opened
// correctly or print out an error message if not and exit.
void errorCheck(DIR *myDir){
    if (myDir == NULL){
        perror("Error");
        exit(1);
    }
}

// buildList(*myDir) -- create a list containing
// all directories' name.
node *buildList(DIR *myDir){
    struct dirent *dirPtr;
    node *temp = NULL;

    // put into list
    while ((dirPtr = readdir(myDir)) != NULL){
        temp = malloc(sizeof(node));
        if (temp == NULL){
            fprintf(stderr, "Error: out of memory!\n");
            exit(1);
        }
        temp -> key = dirPtr -> d_name;
        temp -> next = list;
        list = temp;
    }

    return temp;
}

// printList(*list) -- print out the linked list.
void printList(node *list){
    node *temp = NULL;

    temp = list;
    while (temp != NULL){
        printf("%s\n", temp -> key);
        temp = temp -> next;
    }
}

// swap(*n1, *n2) -- swap keys of these two nodes.
void swap(node *n1, node *n2){
    char *temp;
    temp = n1 -> key;
    n1 -> key = n2 -> key;
    n2 -> key = temp;
}

// sortList(*list) -- use bubble sort to sort the
// linked list in alphabetic order.
void sortList(node *list){
    node *curr, *last = NULL;
    int changed;

    do{
        changed = 0;
        curr = list;

        while (curr -> next != last){
            if (strcmp(curr -> key, curr -> next -> key) > 0){
                swap(curr, curr -> next);
                changed = 1;
            }
            curr = curr -> next;
        }
        last = curr;
    } while (changed == 1);
}

// freeList() -- free up the linked list.
void freeList(){
    node *temp = list;

    while (list != NULL){
        temp = list;
        list = list -> next;
        free(temp -> key);
        free(temp);
    }
}

// main() -- main structure of the program
int main(int argc, char *argv[]){
    DIR *myDir;

    if (argc == 1){
        myDir = opendir(".");
        errorCheck(myDir);
        list = buildList(myDir);
        sortList(list);
        printList(list);
        closedir(myDir);
    }
    else if(argc == 2){
        myDir = opendir(argv[1]);
        errorCheck(myDir);
        list = buildList(myDir);
        sortList(list);
        printList(list);
        closedir(myDir);
    }
    else{
        fprintf(stderr, "Error: too many arguments!\n");
        return 1;
    }

    freeList();
    return 0;
}

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

我没有明确地看到temp-&gt;键的内存分配,所以我的猜测是你不应该在它上面调用free()。​​

// freeList() -- free up the linked list.
void freeList(){
    node *temp = list;

    while (list != NULL){
        temp = list;
        list = list -> next;
        **free(temp -> key);**
        free(temp);
    }

来自http://man7.org/linux/man-pages/man3/readdir.3.html

的信息
   On success, readdir() returns a pointer to a dirent structure.  (This
   structure may be statically allocated; do not attempt to free(3) it.)