我有
int main(void){
while (1){
char *input = NULL;
char buf[1000];
int read;
size_t len;
read = getline(&input, &len, stdin);
if (-1 != read){
sprintf(buf, "%s", input);
lowercase(buf);; // custom function
get_command(buf); // custom function already checked for memory leak
}
free(stdin);
free(input);
free(buf);
}
return 0;
}
通过 valgrind 运行它返回:
HEAP SUMMARY
by 0x4022c2: main(prog.c:647) // read = getline(&input, &len, stdin);
LEAK SUMMARY
still reachable: 120 bytes in 1 blocks
既然我释放了所有内容(stdin、输入、buf),为什么它仍然给我内存泄漏?我该如何解决这个问题?
最佳答案
在评论中,您说您从函数 get_command
中调用 exit(0)
。对 exit(x)
的调用就像程序从 main
返回 x
一样。这意味着您跳过 main
末尾的清理代码。
为了解决此问题,您可以从 get_command
返回一个值,例如 0 表示常规操作,-1 表示错误,1 表示输入结束。这意味着 get_command
中的 exit(0)
现在变为 return 1
。
您的main
循环可能如下所示:
int main(void)
{
char *input = NULL;
size_t len = 0;
while (1) {
if (getline(&input, &len, stdin) < 0) break;
lowercase(input);
if (get_command(input) == 1) break;
}
free(input);
return 0;
}
请注意,我已经修复了您的代码的一些其他问题:
- 不要使用固定大小的临时缓冲区。
getline
可以读取任意长度的行。如果将它们复制到临时缓冲区,至少要确保它适合;毕竟,您已经获得了len
信息。直接使用input
字符串就更好了。 - 每次调用
getline
时,不要以NULL
缓冲区开始。这将减少分配次数,因为只有当一行比之前读取的每一行都长时才会分配新内存,而这种情况不应该经常发生。这也意味着free
应该在循环之后。
关于c - 如何修复内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30013560/