Pos*_*ine 0 c++ valgrind memory-leaks new-operator fread
我从以下函数中获取内存泄漏:
int ReadWrite(int socket, char *readfile) {
FILE *rf = NULL;
rf = fopen(readfile, "rb");
fseek(rf, 0, SEEK_END);
int len = ftell(rf);
rewind(rf);
char readbuf[len + 1];
int res = fread(readbuf, len, 1, rf);
readbuf[len + 1] = '\0';
fclose(rf);
while (1) {
int wres = write(socket, readbuf, res);
if (wres == 0) {
cerr << "socket closed prematurely" << endl;
close(socket);
return EXIT_FAILURE;
}
if (res == -1) {
if (errno == EINTR)
continue;
cerr << "socket write failure: " << strerror(errno) << endl;
close(socket);
return EXIT_FAILURE;
}
break;
}
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
Valgrind告诉我通过这个操作泄漏readfile中的字节数(实际文件,而不是readfile的名称):
Address 0x4c3b67e is 0 bytes after a block of size 14 alloc'd
at 0x4A07C84: operator new[](unsigned long) (vg_replace_malloc.c:363)
Run Code Online (Sandbox Code Playgroud)
令我困惑的是我在代码中没有使用new [].我检查了fopen,ftell和fread,看看他们是否隐藏了"gotcha's",他们在某处调用new [],但在cplusplus.com的文档中没有找到任何内容.我已经尝试了新的char []/delete [],malloc/free和stack-allocated变量(上面)的所有不同组合,但每次都得到相同的valgrind消息.有任何想法吗?谢谢.
| 归档时间: |
|
| 查看次数: |
218 次 |
| 最近记录: |