c - Valgrind 显示块肯定会丢失记录
问题描述
我不明白为什么这段代码有内存泄漏。
数据结构:
typedef struct linkedListNode_t {
int value;
struct linkedListNode_t* next;
struct linkedListNode_t* prev;
}linkedListNode_t;
typedef struct linkedList_t {
unsigned int size;
struct linkedListNode_t* head;
}linkedList_t;
typedef struct hashtableEntry_t {
struct linkedList_t* list;
} hashtableEntry_t;
typedef struct hashtable_t {
unsigned int size;
struct hashtableEntry_t** entry;
} hashtable_t;
typedef struct rbtNode_t {
int value;
char color;
struct rbtNode_t* parent;
struct rbtNode_t* left;
struct rbtNode_t* right;
} rbtNode_t;
typedef struct rbt_t {
unsigned int size;
struct rbtNode_t* root;
struct rbtNode_t* nil;
} rbt_t;
我使用的免费功能:
void linkedListFree(linkedList_t* list) {
linkedListNode_t *node = list->head;
linkedListNode_t *victim;
while (node != NULL) {
victim = node;
node = node->next;
free(victim);
}
list->head=NULL;
free(list);
}
void hashtableFree(hashtable_t* hashtbl) {
int i;
if(!hashtbl)
return;
for(i=0;i<hashtbl->size;i++){
linkedListFree(hashtbl->entry[i]->list);
free(hashtbl->entry[i]);
hashtbl->entry[i]=NULL;
}
free(hashtbl);
hashtbl=NULL;
}
void rbtFreeNodes(rbt_t* T, rbtNode_t* x) {
if(x == T->nil)
return;
rbtFreeNodes(T,x->left);
rbtFreeNodes(T,x->right);
free(x);
}
void rbtFree(rbt_t* T) {
rbtFreeNodes(T,T->root);
free(T);
T = NULL;
}
这是 Valgrind 给我的信息:
==28711== 76,800 bytes in 2,400 blocks are definitely lost in loss record 4 of 5
==28711== at 0x483C7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==28711== by 0x10A026: createRbt
==28711== by 0x10AB76: doExperiment
==28711== by 0x1094BA: main
==28711==
==28711== 1,132,800 bytes in 2,400 blocks are definitely lost in loss record 5 of 5
==28711== at 0x483C7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==28711== by 0x109BBE: createHashtable
==28711== by 0x10AA89: doExperiment
==28711== by 0x10949D: main
这是我的 doExperiment 函数:
clock_t doExperiment(int* randomArray, const unsigned int numInsertions, const unsigned int numSearches, char* dataStructure) {
clock_t startTime=0, endTime=0,tot = 0;
if(strcmp(dataStructure,"hashtable")==0){
hashtable_t *ht = createHashtable(NUM_ENTRIES);
linkedListNode_t *nodo;
startTime = clock();
for(int i=0;i<numInsertions;i++){
int randomIndex = rand() % numInsertions;
hashtableInsert(ht,randomArray[randomIndex]);
}
for(int i=0; i<numSearches;i++){
int randomIndex = rand() % numInsertions;
nodo = hashtableSearch(ht,randomArray[randomIndex]);
}
endTime = clock();
tot = endTime-startTime;
hashtableFree(ht);
}
if(strcmp(dataStructure,"rbt")==0){
struct rbt_t *rbt = createRbt();
struct rbtNode_t *nodo;
startTime = clock();
for(int i=0;i<numInsertions;i++){
int randomIndex = rand() % numInsertions;
nodo = createRbtNode(randomArray[randomIndex]);
rbtInsert(rbt,nodo);
}
for(int i=0;i<numSearches;i++){
int randomIndex = rand() % numInsertions;
nodo = rbtSearch(rbt,randomArray[randomIndex]);
}
endTime = clock();
tot = endTime-startTime;
rbtFree(rbt);
}
return tot;
}
正如你所看到的,我总是调用免费函数,但我仍然有记忆丧失。我不知道我的错误在哪里。我的免费功能是否正确?或者他们不释放数据结构?非常感谢任何形式的帮助或建议。
解决方案
推荐阅读
- c# - 如何解决以下场景中的类型转换错误
- python - 从字典中返回一组具有共同值的键对
- css - 如何使用anchorOrigin使Material-UI Snackbar不占用整个屏幕宽度?
- python - 如何在拆分中添加条件应用组合并在每一行上重复解决方案?
- angular - Angular中的FromEvent
- dotnetbrowser - dotnetbrowser - 铬版本升级
- javascript - 如何在多个文本上添加计数器
- java - 为什么快速排序需要递归“排序”小节?
- python - Python3——对象路径……坏了?
- npm - npm publish 给出“无范围的包不能是私有的”