[英]C - Can't find memory leaks (Valgrind)
I'm new at covering memory leaks and I'm concerned about the problem of leaks.我是 memory 泄漏的新手,我很担心泄漏问题。 I'm using Valgrind.
我正在使用 Valgrind。
I've solved most of it ye't I cant pinpoint where the last (14) leaks come from as I feel I'm freeing all allocated memory.我已经解决了大部分问题,但我无法确定最后 (14) 个泄漏的来源,因为我觉得我正在释放所有分配的 memory。
I've tried to go through all my mallocs/callocs without success.我已经尝试通过我所有的 mallocs/callocs go 但没有成功。 I've debugged for a while but no progress.
我已经调试了一段时间,但没有任何进展。
HEAP SUMMARY:
==9664== in use at exit: 85 bytes in 14 blocks
==9664== total heap usage: 105 allocs, 91 frees, 182,137 bytes allocated
==9664==
==9664== 85 bytes in 14 blocks are definitely lost in loss record 1 of 1
==9664== at 0x483877F: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
void readline(FILE *file) {
char line[120];
while (fgets(ligne, 81, file)) {
replace_words(line, words, &i);
}
}
Within the function placermots_tabs
there is used the function strdup
that dynamically allocates memory and returns a pointer to the allocated memory. And the allocated memory was not freed when the created character array by the function strdup was not required any more.在 function
placermots_tabs
中使用了 function strdup
,它动态分配 memory 并返回指向分配的 memory 的指针。当 8835480886 创建的字符数组不再需要时,分配的 memory 没有被释放。
Here is the function这是function
void placermots_tabs(char ligne[80], char **words, int *i) {
char *word = strtok(ligne, " ,.-\n");
while (word != NULL) { // strtok result controls the loop
words[(*i)++] = strdup(word);
word = strtok(NULL, " ,.-\n");
}
}
Within the while loop there are dynamically allocated character arrays using the function strdup
.在 while 循环中,使用 function
strdup
动态分配字符 arrays。
words[(*i)++] = strdup(word);
You need to track correctly the allocated memory: whether it was freed.您需要正确跟踪分配的 memory:它是否已被释放。
It seems the reason of the memory leak is the while loop within the function lire_lignes
似乎 memory 泄漏的原因是 function
lire_lignes
中的 while 循环
void lire_lignes(FILE *file, char **words, int *nb_mots, struct Stats *stats) {
int i = 0;
char ligne[80];
while (fgets(ligne, 81, file)) {
placermots_tabs(ligne, words, &i);
}
effacer_doublons(nb_mots, words);
stats->mot_sans_doublons = *nb_mots;
trouver_lettre_frequente((char const **) words, stats);
fclose(file);
}
where the function placermots_tabs
is called that overrides anew the array pointed to by the pointer words
.调用 function
placermots_tabs
的位置会重新覆盖指针words
指向的数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.