下面是创建动态结构数组的代码。

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

typedef struct
{
    int flag;
    char* ip;
} ip_mon;

typedef struct {
    ip_mon *array;
    size_t nItems;
    size_t size;
    size_t block_size;
} item_list;

item_list* unsorted;

item_list* create_list(size_t block_size) {
    item_list* pList = malloc(sizeof(item_list));
    pList->array = (ip_mon *)malloc(block_size * sizeof(ip_mon));
    pList->nItems = 0;
    pList->size = block_size;
    return pList;
}

void delete_list(item_list* pList) {
    int i;
    for(i=0; i<pList->nItems; i++)
    {
        free(pList->array[i].ip);
    }
    free(pList->array);
    free(pList);
}


void add_to_list(item_list* pList, char *word)
{
    if (pList->nItems == pList->size)
    {
        pList->size *= 2;
        pList->array = (ip_mon *)realloc(pList->array, pList->size * sizeof(ip_mon));
    }
    pList->array[pList->nItems].ip = strdup(word);
    pList->nItems++;
}


int load_items(char *file, item_list* pList)
{
    FILE *file_handle;
    char nutt2[4096];

    if((file_handle=fopen(file,"r"))==NULL) {
        printf("[!] FATAL: Cannot open %s \n", file);
        return -1;
    } else {
        while (fgets(nutt2,sizeof(nutt2),file_handle)){
            char *temp;
            temp = strdup (nutt2);
            temp = strtok (temp, "\n");
            add_to_list(pList, temp);
            free(temp);
        } fclose(file_handle);
        printf("[!] INFO: File %s loaded.\n", file);
        return 1;
    }
    return 0;
}

int load_text(char *file)
{
    FILE *filecheck;
    if(unsorted != NULL){
        delete_list(unsorted);
        unsorted = create_list(2);
        if(file != NULL){
            if((filecheck=fopen(file,"r"))!=NULL)
                load_items(file, unsorted);
            else {
                printf("file %s doesn't exists.\n", file);
                return -1;
            }
        }
        return 1;
    } else {
        unsorted = create_list(2);
        if(file != NULL){
            if((filecheck=fopen(file,"r"))!=NULL)
                load_items(file, unsorted);
            else {
                printf("file %s doesn't exists.\n", file);
                return -1;
            }
        }
        return 1;
    }
    return 0;
}

int show_list_items(item_list* pList){
    int iterItem;
    if(pList != NULL){
        for (iterItem = 0; iterItem < pList->nItems; ++iterItem) {
            printf("%s\n", pList->array[iterItem].ip);
        }
        return 1;
    }
    return 0;
}

int main(){
    if(load_text("inf.txt")){
        show_list_items(unsorted);
        delete_list(unsorted);
    }
    return 0;
}

一切都很好,但是在用valgrind检查编译代码是否存在内存泄漏时。我得到以下信息:
==2980== HEAP SUMMARY:
==2980==     in use at exit: 568 bytes in 1 blocks
==2980==   total heap usage: 11 allocs, 10 frees, 1,331 bytes allocated
==2980==
==2980== Searching for pointers to 1 not-freed blocks
==2980== Checked 69,344 bytes
==2980==
==2980== 568 bytes in 1 blocks are still reachable in loss record 1 of 1
==2980==    at 0x4C2A29B: malloc (vg_replace_malloc.c:270)
==2980==    by 0x4E9947A: __fopen_internal (iofopen.c:76)
==2980==    by 0x400AB2: load_text (sort.c:98)
==2980==    by 0x400B68: main (sort.c:122)
==2980==
==2980== LEAK SUMMARY:
==2980==    definitely lost: 0 bytes in 0 blocks
==2980==    indirectly lost: 0 bytes in 0 blocks
==2980==      possibly lost: 0 bytes in 0 blocks
==2980==    still reachable: 568 bytes in 1 blocks
==2980==         suppressed: 0 bytes in 0 blocks

我错过了什么?
我试图避免双重自由,但似乎仍然有一个街区,仍然是可达的。
有什么想法或暗示吗?

最佳答案

内部load_text()

        if((filecheck=fopen(file,"r"))!=NULL)

您在此处打开了文件,但没有fclose(file)

09-25 19:16