[英]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.