首页 > 解决方案 > 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;
    

}

正如你所看到的,我总是调用免费函数,但我仍然有记忆丧失。我不知道我的错误在哪里。我的免费功能是否正确?或者他们不释放数据结构?非常感谢任何形式的帮助或建议。

标签: cbinary-search-treehashtablevalgrinddoubly-linked-list

解决方案


推荐阅读