Я пытаюсь улучшить некрасивый код C, который вызывает утечку памяти. Точки Valgrind:
==19046== 1,001 bytes in 1 blocks are definitely lost in loss record 1 of 1
==19046== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==19046== by 0x109D0B: save_params (ugly.c:188)
save_params очень длинный, но после удаления других частей его можно представить так:
/* Save params to file */
int save_params(int nb_iter) {
char *word = malloc(sizeof(char) * MAX_STRING_LENGTH + 1); // <----- ugly.c:188 line
FILE *fid, *fout, *fgs;
for (a = 0; a < vocab_size; a++) {
if (fscanf(fid,format,word) == 0) {free(word); return 1;}
if (strcmp(word, "<unk>") == 0) {free(word); return 1;}
fprintf(fout, "%s",word);
//...
if (save > 0) {
fprintf(fgs, "%s",word);
if (fscanf(fid,format,word) == 0) {free(word); return 1;}
}
if (use_unk_vec) {
word = "<unk>";
fprintf(fout, "%s",word);
}
fclose(fid);
fclose(fout);
if (save > 0) fclose(fgs);
}
free(word); <--------- that free is "invalid"
return 0;
}
Выход Valgrind:
==19046== Invalid free() / delete / delete[] / realloc()
==19046== by 0x10AAC6: save_params (ugly.c:288)
==19046==
==19046== HEAP SUMMARY:
==19046== in use at exit: 1,001 bytes in 1 blocks
==19046== total heap usage: 78 allocs, 78 frees, 13,764,515 bytes allocated
==19046==
==19046== 1,001 bytes in 1 blocks are definitely lost in loss record 1 of 1
==19046== by 0x109D0B: save_params (ugly.c:188)
==19046== LEAK SUMMARY:
==19046== definitely lost: 1,001 bytes in 1 blocks
==19046== indirectly lost: 0 bytes in 0 blocks
==19046==
==19046== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
Не могли бы вы подсказать, что я делаю не так? Как мне освободить память, зарезервированную mallo c?