c - closedir() 后无效的 free()

标签 c linux memory-management memory-leaks

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

******:~/***/***/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;
}

有人能帮忙吗?

最佳答案

我没有明确地看到 temp->key 的内存分配在哪里,所以我猜你不应该在上面调用 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.)

关于c - closedir() 后无效的 free(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36366951/

相关文章:

c# - 如何全屏显示消息?

c++ - 您发现自己在 iOS 开发中使用了哪些 C/C++ 库?

c++ - 将字符串添加到文件缓冲区,用于 cURL smtp 发送电子邮件的消息正文

python - 单元测试文件修改

python - Python 中如何为 `list` 分配内存?为什么列表的大小与其对象的总和不同?

linux - Linux 上的虚拟内存

macos - 如何在 Mac OSX 上检查 Matlab 2010b 或更高版本中的可用内存?

c - 使用 scanf 和 r+ 文件时出现段错误(核心转储)

c - 空数组怎么能参与计算呢?

linux - 解释 Linux 内核状态术语,例如 net.next、linux-next、net.git