如何解决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,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;
}
正如你所看到的,我总是调用免费函数,但我仍然有记忆丢失。 我无法弄清楚我的错误在哪里。 我的免费功能是否正确?或者他们不释放数据结构?任何形式的帮助或建议都非常感谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。