Edit : the problem isn't what I thought it was, check answers before reading my (long) question will spare you some time.
编辑:问题不是我想的那样,在阅读我的(长期)问题之前检查答案将为您节省一些时间。
i've been valgrinding my little C project for school (basically a language detector), but there's a set of pointers I still have problems with according to valgrind. I think the problem is classic, it could be resumed in : How should I free a variable i malloced in another function when I (think I) have no choice ?
我一直在学习我的小C项目(基本上是语言检测器),但根据valgrind的说法,我仍然有一些指针。我认为问题是经典的,它可以恢复:当我(我想)别无选择时,我应该如何释放我在另一个函数中进行malloced的变量?
Here are the relevant parts of the two functions which cause me this problem. Basically, get_modeles_names
gathers the filenames holding the right substring within them from my "collection" file; then det_langue
is processing them. Here is get_modeles_names :
以下是导致这个问题的两个函数的相关部分。基本上,get_modeles_names从我的“集合”文件中收集包含其中正确子字符串的文件名;然后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] = '\0';
//replaces the final '\n' by '\0'
i++;
line = NULL;
}
}
if (line)
free(line);
fclose(collection);
}
And here is det_langue :
这是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);
}
As you can see, i malloc my char ** modeles in det_langue to the right size (i am confident the nb_modeles function works fine) , then i malloc each element of modeles in get_modeles_names ; but since i need to process them later on, i can't free them (or don't know how) where i malloced them, i free them later, in the end of det_langue. I would think it's allright, but here's the output of valgrind --leak-check=full ./projet -d pomme -m 1
正如你所看到的,我在det_langue中将我的char **模型malloc到正确的大小(我确信nb_modeles函数工作正常),然后我在get_modeles_names中malloc每个元素元素;但是因为我需要稍后处理它们,所以我无法释放它们(或者不知道如何)我在哪里使用它们,我们在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)
Line 120 is in get_modeles_names, it's : modeles[i] = (char*) malloc(strlen(line) * sizeof (char));
Also, the size of modeles is 4, which explains for the 40 allocs but only 36 frees. Also, if I go berserk and simply free modeles in the get_modeles_names
function, my program obviously seg_faults, since the data i'm supposed to process don't exist anymore.
第120行在get_modeles_names中,它是:modeles [i] =(char *)malloc(strlen(line)* sizeof(char));此外,模型的大小为4,这解释了40个分配但只有36个释放。另外,如果我在get_modeles_names函数中进行狂暴和简单的免费模型,我的程序显然是seg_faults,因为我应该处理的数据不再存在。
I've googled around, but couldn't find any question already asked which would answer mine .... any idea ?
我已经用Google搜索了,但是找不到任何已经问过的问题,哪个会回答我的....有什么想法吗?
Edit : the mem_leak was the one described in solution, but there is another as soon as it's solved : line leaks, which is solved by moving
编辑:mem_leak是解决方案中描述的那个,但是一旦解决就会有另一个:线路泄漏,这是通过移动解决的
if(line)
free(line);
within the while loop.
在while循环中。
2 个解决方案
#1
2
I see the following problem:
我看到以下问题:
if (strstr(line, entete)) {
modeles[i] = (char*) malloc(strlen(line) * sizeof (char));
modeles[i] = line;
Here, you first allocate memory with malloc
and save the pointer to it in modeles[i]
but then you overwrite the pointer with line
. If you want to copy the contents of line
, then use strcpy
.
在这里,首先使用malloc分配内存并在modeles [i]中保存指向它的指针,然后用线覆盖指针。如果要复制行的内容,请使用strcpy。
Note that you do not allocate enough memory for the strcpy
of line
: you forget the terminating null character. It should be:
请注意,您没有为strcpy of line分配足够的内存:您忘记了终止空字符。它应该是:
modeles[i] = malloc(strlen(line) + 1);
or:
要么:
int len= strlen(line);
line[len-2] = '\0';
modeles[i] = malloc(len);
strcpy(modeles[i], line);
.. and sizeof(char)
is not needed because sizeof(char)
is always 1.
因为sizeof(char)始终为1,所以不需要..和sizeof(char)。
#2
1
The problem is
问题是
modeles[i] = line;
that is just replacing the pointer to the memory you malloc'd You want a strcpy() there:
这只是替换指向malloc的内存的指针你想要一个strcpy():
strcpy(models[i], line);
#1
2
I see the following problem:
我看到以下问题:
if (strstr(line, entete)) {
modeles[i] = (char*) malloc(strlen(line) * sizeof (char));
modeles[i] = line;
Here, you first allocate memory with malloc
and save the pointer to it in modeles[i]
but then you overwrite the pointer with line
. If you want to copy the contents of line
, then use strcpy
.
在这里,首先使用malloc分配内存并在modeles [i]中保存指向它的指针,然后用线覆盖指针。如果要复制行的内容,请使用strcpy。
Note that you do not allocate enough memory for the strcpy
of line
: you forget the terminating null character. It should be:
请注意,您没有为strcpy of line分配足够的内存:您忘记了终止空字符。它应该是:
modeles[i] = malloc(strlen(line) + 1);
or:
要么:
int len= strlen(line);
line[len-2] = '\0';
modeles[i] = malloc(len);
strcpy(modeles[i], line);
.. and sizeof(char)
is not needed because sizeof(char)
is always 1.
因为sizeof(char)始终为1,所以不需要..和sizeof(char)。
#2
1
The problem is
问题是
modeles[i] = line;
that is just replacing the pointer to the memory you malloc'd You want a strcpy() there:
这只是替换指向malloc的内存的指针你想要一个strcpy():
strcpy(models[i], line);