2014-01-12 2 views
-1

valgrind 및 realloc을 사용하는 데 문제가 있습니까? valgrind는 메모리에 대한 오류를 제공하는 것으로 보입니다.하지만 해제되지 않은 모든 것을 해제한다고 믿어도 해제되지는 않습니다. 터미널에서Realloc 및 Valgrind

/* declarations*/ 
typedef struct Hash_node{ 
    char* word;   
    char** overflow_list; 
    int overlow; 
} Hash_node; 

Hash_node* create_Hash_node(); 
int assign_word(Hash_node*,char*); 
void free_Hash_node(Hash_node*); 

Hash_node* create_Hash_node(char* word){  
    Hash_node *temp=malloc(sizeof(Hash_node)); 
    if(temp==NULL){ 
     printf("Memory couldn't be allocated at creating hash_node\n"); 
     return NULL; 
    } 

    temp-> word=NULL; 
    temp-> overflow_list=NULL; 
    temp->overlow=0; 

    return temp; 
}; 

void free_Hash_node(Hash_node* node){ 
    if(node->word!=NULL) free(node->word); 

    if(node->overlow > 0){ 
     int i;  
     for(i=0; i< node->overlow;i++){ 
      printf("%d",i); 
      free(node->overflow_list[i]); 
     }   
    } 

    free(node); 
} 

int assign_word(Hash_node* node,char* word){ 

    if(word==NULL) return -1; 

    if(node->word==NULL){ 
     node->word=(char*)malloc(sizeof(char)*(strlen(word)+1)); 
     strcpy(node->word,word); 

     return success; 
    } 

    /*collision exists*/ 
    if(strcmp(word,node->word)==0){ 
     return collision; 
    } else{ 
     node->overlow++; 

     node->overflow_list=realloc(node->overflow_list , node->overlow* sizeof(char*)); 

     node->overflow_list[node->overlow-1] = (char*) malloc(sizeof(char) * (strlen(word) + 1)); 
     strcpy( node->overflow_list[node->overlow-1] ,word); 
     return collision; 
    } 
} 
int main(int argc, char** argv) { 

    Hash_node *a=create_Hash_node(); 
    assign_word(a,"mpla"); 
    assign_word(a,"hell"); 
    assign_word(a,"kitsos2"); 

    free_Hash_node(a); 

    return (EXIT_SUCCESS); 
} 

Valgrind의 인쇄 : 내가 제대로 볼 경우

==12495== HEAP SUMMARY: 
==12495==  in use at exit: 16 bytes in 1 blocks 
==12495== total heap usage: 6 allocs, 5 frees, 66 bytes allocated 
==12495== 
==12495== LEAK SUMMARY: 
==12495== definitely lost: 16 bytes in 1 blocks 
==12495== indirectly lost: 0 bytes in 0 blocks 
==12495==  possibly lost: 0 bytes in 0 blocks 
==12495== still reachable: 0 bytes in 0 blocks 
==12495==   suppressed: 0 bytes in 0 blocks 
==12495== Rerun with --leak-check=full to see details of leaked memory 
==12495== 
==12495== For counts of detected and suppressed errors, rerun with: -v 
==12495== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2) 
+0

글쎄, 확실히 거기에 몇 가지 malloc 오류가 있습니다 : 'realloc'으로 오류를 검사하지 않으면 잠재적 인 메모리 누수입니다. –

답변

4

하면 무료로 모든 항목 node->overflow_list[i], 하지만 목록 자체 : free_Hash_node()에서

free(node->overflow_list) 

.

+0

오, 그래 ... 잊어 버렸어. 정말 고마워. 최대한 빨리 답변을 확인해 드리겠습니다. – JmRag