为什么我无法释放内存?无效的 free() / delete / delete[] / realloc()
Why I am not able to free memory? Invalid free() / delete / delete[] / realloc()
我正在尝试改进丑陋的 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)
你能告诉我我做错了什么吗?我应该如何释放 malloc 保留的内存?
你不能释放它,因为你做了:
word = "<unk>";
现在word
不再指向你分配的内存,它指向这个文字串。你有内存泄漏,当你试图释放它时出错。
没有必要在那里重新分配 word
。只需将文字字符串写入文件即可。
if (use_unk_vec) {
fprintf(fout, "<unk>");
}
也不清楚为什么首先需要使用 malloc()
和 free()
。由于您在此函数中仅使用 word
,因此您应该将其声明为本地数组:
char word[MAX_STRING_LENGTH + 1];
我正在尝试改进丑陋的 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)
你能告诉我我做错了什么吗?我应该如何释放 malloc 保留的内存?
你不能释放它,因为你做了:
word = "<unk>";
现在word
不再指向你分配的内存,它指向这个文字串。你有内存泄漏,当你试图释放它时出错。
没有必要在那里重新分配 word
。只需将文字字符串写入文件即可。
if (use_unk_vec) {
fprintf(fout, "<unk>");
}
也不清楚为什么首先需要使用 malloc()
和 free()
。由于您在此函数中仅使用 word
,因此您应该将其声明为本地数组:
char word[MAX_STRING_LENGTH + 1];