我有一个哈希表存储实现,其中存储了 Entry 的 typedef 结构,并且有一个函数可以查询整个表以查看条目的数据是否与一组特定条件匹配。我实现这个的方式是服务器利用
条目 **queryReturns = tableQuery(params);
为了调用函数,函数为返回的Entry指针数组分配内存。
(在tableQuery函数中:)
Entry **tableQuery(params) {
//Process params and get expected results
Entry **returnEntries = (Entry *)malloc(j * sizeof(Entry *));
//Code to copy pointers into the array
return returnEntries;
}
当服务器函数用这个完成时,我应该只用它解除分配
free(queryReturns);
因为我不想删除我正在存储的实际数据元素,所以我只想释放这个临时的 Entry 指针数组,但是 Valgrind 产生了这条消息:
205,816 bytes in 96 blocks are definitely lost in loss record 17 of 17
at 0x4C28BED: malloc (vg_replace_malloc.c:263)
by 0x4086D1: tableQuery (Table.c:798)
by 0x403808: handle_command (server.c:645)
by 0x403F05: threadCall (server.c:799)
by 0x52B3B4F: start_thread (pthread_create.c:304)
我应该担心这会导致内存泄漏,还是应该忽略它?
最佳答案
Should I worry about this causing memory leaks
是的:这确实是内存泄漏,您应该担心它。
很遗憾,您没有告诉我们这一行是否:
Entry **returnEntries = (Entry *)malloc(j * sizeof(Entry *));
是 Valgrind 提示的分配行(Table.c:798
)。
如果是,那么至少有一个 tableQuery()
的调用者实际上没有像它应该的那样释放
返回的数组。
关于c - Valgrind 和释放特殊指针数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22854064/