需要帮助来检测额外的 malloc - CS50 Pset5

Need help to detect extra malloc - CS50 Pset5

Valgrind 说丢失了 0 个字节,但也说比 Mallocs 少一个 Frees
因为我只使用过一次 malloc,所以我只发布这些段而不是所有 3 个文件。

将 dictionary.txt 文件加载到散列中时 table:

bool load(const char *dictionary)
{
   (dictionary.c:54) FILE *dict_file = fopen(dictionary, "r");
    if (dict_file == NULL)
        return false;
    int key; 
    node *n = NULL;
    int mallocs = 0;
    while (1)
    { 
        n = malloc(sizeof(node));
        printf("malloced: %i\n", ++mallocs);
        if (fscanf(dict_file, "%s", n->word) == -1)
        { 
            printf("malloc freed\n"); 
            free(n); 
            break;
        }
        key = hash(n->word);
        n->next = table[key];
        table[key] = n;
        words++;
    }
    return true;
}

卸载部分:

bool unload(void)
{
    int deleted = 0;
    node *n;
    for (int i = 0; i < N; i++)
    {
        n = table[i];
        while(n != NULL)
        {
            n = n->next;
            free(table[i]);
            table[i] = n;
            deleted++;
        }
    }
    printf("DELETED: %i", deleted);
    return true;
}

Check50 表示存在内存泄漏。但是看不懂在哪里。

Command: ./speller dictionaries/small texts/cat.txt
==4215== 
malloced: 1
malloced: 2
malloced: 3
malloced: 4
malloc freed

DELETED: 3
WORDS MISSPELLED:     2
WORDS IN DICTIONARY:  3
WORDS IN TEXT:        6
TIME IN load:         0.03
TIME IN check:        0.00
TIME IN size:         0.00
TIME IN unload:       0.00
TIME IN TOTAL:        0.03
==4215== 
==4215== HEAP SUMMARY:
==4215==     in use at exit: 552 bytes in 1 blocks
==4215==   total heap usage: 9 allocs, 8 frees, 10,544 bytes allocated
==4215== 
==4215== 552 bytes in 1 blocks are still reachable in loss record 1 of 1
==4215==    at 0x4C31B0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4215==    by 0x525AF29: __fopen_internal (iofopen.c:65)
==4215==    by 0x525AF29: fopen@@GLIBC_2.2.5 (iofopen.c:89)
==4215==    by 0x40114E: load (dictionary.c:54)
==4215==    by 0x40095E: main (speller.c:40)
==4215== 
==4215== LEAK SUMMARY:
==4215==    definitely lost: 0 bytes in 0 blocks
.
.
.
==4215== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

speller.c 有分配码。我希望问题的其余部分是清晰易懂的。

需要关闭指向已打开文件 (dict_file) 的指针。参见 man fclose