[英]C - Can't find memory leaks (Valgrind)
我是 memory 泄漏的新手,我很担心泄漏问题。 我正在使用 Valgrind。
我已经解决了大部分问题,但我无法确定最后 (14) 个泄漏的来源,因为我觉得我正在释放所有分配的 memory。
我已经尝试通过我所有的 mallocs/callocs go 但没有成功。 我已经调试了一段时间,但没有任何进展。
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);
}
}
在 function placermots_tabs
中使用了 function strdup
,它动态分配 memory 并返回指向分配的 memory 的指针。当 8835480886 创建的字符数组不再需要时,分配的 memory 没有被释放。
这是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");
}
}
在 while 循环中,使用 function strdup
动态分配字符 arrays。
words[(*i)++] = strdup(word);
您需要正确跟踪分配的 memory:它是否已被释放。
似乎 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);
}
调用 function placermots_tabs
的位置会重新覆盖指针words
指向的数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.