正确释放在其他函数中分配的内存 space

Properly freeing memory space malloced in other function

编辑:问题不是我想的那样,在阅读我的(长)问题之前检查答案会节省你一些时间。

我一直在为学校验证我的小 C 项目(基本上是一个语言检测器),但是根据 valgrind,我仍然有一组指针存在问题。我认为这个问题很经典,可以在以下内容中恢复:当我(认为我)别无选择时,我应该如何释放我在另一个函数中分配的变量?

以下是导致我出现此问题的两个函数的相关部分。基本上,get_modeles_names 从我的 "collection" 文件中收集包含正确子字符串的文件名;然后 det_langue 正在处理它们。 这是 get_modeles_names :

    void get_modeles_names(int mode, char ** modeles)
{
        FILE * collection = get_collection_file();
        int i = 0;
        char * line = NULL;
        size_t size = 0;

        while (getline(&line, &size, collection) != -1) {
            if (strstr(line, entete)) {
                modeles[i] = (char*) malloc(strlen(line) * sizeof (char));
                modeles[i] = line;

                modeles[i][strlen(modeles[i]) - 1] = '[=11=]';
                //replaces the final '\n' by '[=11=]'

                i++;
                line = NULL;
            }
        }

        if (line)
            free(line);

        fclose(collection);
}

这里是 det_langue :

void det_langue(char * w)
{
    int nb_modele = nb_modeles();

    char ** modeles = (char **) malloc(nb_modele * sizeof (char *));
    get_modeles_names(mode, modeles);

    double * resultats = (double *) malloc(nb_modele * sizeof (double));

    int i;
    for (i = 0; i < nb_modele; i++) {
        resultats[i] = calculate_result (w, modeles[i]);
        }
    }

    for (i = 0; i < nb_modele; i++) {
        free(modeles[i]);
    }
    free(modeles);
    free(resultats);
}

如您所见,我将 det_langue 中的 char ** modeles malloc 到正确的大小(我相信 nb_modeles 函数工作正常),然后我 malloc modeles 中的每个元素get_modeles_names;但是因为我需要稍后处理它们,所以我无法在我分配它们的地方释放它们(或者不知道如何),我稍后释放它们,在 det_langue 结束时。我认为没关系,但这是 valgrind --leak-check=full ./projet -d pomme -m 1

的输出
==30547== Command: ./projet -d pomme -m 1
==30547== 
pomme -> french
==30547== 
==30547== HEAP SUMMARY:
==30547==     in use at exit: 113 bytes in 4 blocks
==30547==   total heap usage: 40 allocs, 36 frees, 30,906 bytes allocated
==30547== 
==30547== 113 bytes in 4 blocks are definitely lost in loss record 1 of 1
==30547==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==30547==    by 0x40163E: get_modeles_names (gestion_collection_modeles.c:120)
==30547==    by 0x401919: det_langue (det_langue.c:12)
==30547==    by 0x40189E: main (Main.c:76)
==30547== 
==30547== LEAK SUMMARY:
==30547==    definitely lost: 113 bytes in 4 blocks
==30547==    indirectly lost: 0 bytes in 0 blocks
==30547==      possibly lost: 0 bytes in 0 blocks
==30547==    still reachable: 0 bytes in 0 blocks
==30547==         suppressed: 0 bytes in 0 blocks
==30547== 
==30547== For counts of detected and suppressed errors, rerun with: -v
==30547== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

第 120 行在 get_modeles_names 中:modeles[i] = (char*) malloc(strlen(line) * sizeof (char)); 此外,modeles 的大小为 4,这解释了 40 个分配但只有 36 个释放。 此外,如果我发狂并在 get_modeles_names 函数中简单地释放模型,我的程序显然 seg_faults,因为我应该处理的数据不再存在。

我用谷歌搜索了一下,但找不到任何可以回答我的问题......有什么想法吗?

编辑:mem_leak 是解决方案中描述的那个,但解决后还有另一个:线路泄漏,通过移动

解决
if(line)
    free(line);

在 while 循环中。

问题是

modeles[i] = line;

这只是替换指向您 malloc 的内存的指针 你想要一个 strcpy() 那里:

strcpy(models[i], line);

我看到以下问题:

        if (strstr(line, entete)) {
            modeles[i] = (char*) malloc(strlen(line) * sizeof (char));
            modeles[i] = line;

在这里,您首先使用 malloc 分配内存并将指向它的指针保存在 modeles[i] 中,然后您使用 line 覆盖指针。如果要复制line的内容,则使用strcpy.

请注意,您没有为 linestrcpy 分配足够的内存:您忘记了终止空字符。应该是:

            modeles[i] = malloc(strlen(line) + 1);

或:

            int len= strlen(line);
            line[len-2] = '[=12=]';
            modeles[i] = malloc(len);
            strcpy(modeles[i], line);

.. 不需要 sizeof(char) 因为 sizeof(char) 总是 1.