我正在做一个程序,我有一个哈希表,其中元素具有这种类型的结构
typedef struct _msg_list{
message_t *msg;
struct _msg_list *next;
}msg_list;
typedef struct _hash_elem{
char *nickname;
int nmsg;
msg_list *msg_head;
msg_list *msg_corr;
}hash_elem;
其中msg_list是指向接收消息列表的指针。消息具有这种结构。
typedef struct {
int op;
char sender[33];
} message_hdr_t;
typedef struct {
char receiver[33];
unsigned int len;
} message_data_hdr_t;
typedef struct {
message_data_hdr_t hdr;
char *buf;
} message_data_t;
typedef struct {
message_hdr_t hdr;
message_data_t data;
} message_t;
当我调用freeHashData函数来清理内存时,valgrind给我这个输出
==4709== Memcheck, a memory error detector
==4709== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==4709== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==4709== Command: ./chatty
==4709==
==4709==
==4709== HEAP SUMMARY:
==4709== in use at exit: 98 bytes in 4 blocks
==4709== total heap usage: 14 allocs, 10 frees, 8,622 bytes allocated
==4709==
==4709== 16 bytes in 1 blocks are indirectly lost in loss record 1 of 4
==4709== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4709== by 0x108F0A: insertMsg (in /home/giacomo/Scrivania/chatty)
==4709== by 0x1092DB: main (in /home/giacomo/Scrivania/chatty)
==4709==
==4709== 32 (16 direct, 16 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 4
==4709== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4709== by 0x108F0A: insertMsg (in /home/giacomo/Scrivania/chatty)
==4709== by 0x1092C8: main (in /home/giacomo/Scrivania/chatty)
==4709==
==4709== 33 bytes in 1 blocks are definitely lost in loss record 3 of 4
==4709== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4709== by 0x1090F4: main (in /home/giacomo/Scrivania/chatty)
==4709==
==4709== 33 bytes in 1 blocks are definitely lost in loss record 4 of 4
==4709== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4709== by 0x109166: main (in /home/giacomo/Scrivania/chatty)
==4709==
==4709== LEAK SUMMARY:
==4709== definitely lost: 82 bytes in 3 blocks
==4709== indirectly lost: 16 bytes in 1 blocks
==4709== possibly lost: 0 bytes in 0 blocks
==4709== still reachable: 0 bytes in 0 blocks
==4709== suppressed: 0 bytes in 0 blocks
程序只是在哈希表中注册2个用户,然后我向用户发送消息并将它们存储在消息列表中。
这是主要和其他功能
typedef struct icl_entry_s {
void* key;
void *data;
struct icl_entry_s* next;
} icl_entry_t;
typedef struct icl_hash_s {
int nbuckets;
int nentries;
icl_entry_t **buckets;
unsigned int (*hash_function)(void*);
int (*hash_key_compare)(void*, void*);
} icl_hash_t;
icl_hash_t *hash;
static inline void setHeader(message_hdr_t *hdr, int op, char *sender) {
#if defined(MAKE_VALGRIND_HAPPY)
memset((char*)hdr, 0, sizeof(message_hdr_t));
#endif
hdr->op = op;
strncpy(hdr->sender, sender, strlen(sender)+1);
}
static inline void setData(message_data_t *data, char *rcv, const char *buf, unsigned int len) {
#if defined(MAKE_VALGRIND_HAPPY)
memset((char*)&(data->hdr), 0, sizeof(message_data_hdr_t));
#endif
strncpy(data->hdr.receiver, rcv, strlen(rcv)+1);
data->hdr.len = len;
data->buf = (char *)buf;
}
/**
* A simple string hash.
*
* An adaptation of Peter Weinberger's (PJW) generic hashing
* algorithm based on Allen Holub's version. Accepts a pointer
* to a datum to be hashed and returns an unsigned integer.
* From: Keith Seymour's proxy library code
*
* @param[in] key -- the string to be hashed
*
* @returns the hash index
*/
unsigned int
hash_pjw(void* key)
{
char *datum = (char *)key;
unsigned int hash_value, i;
if(!datum) return 0;
for (hash_value = 0; *datum; ++datum) {
hash_value = (hash_value << ONE_EIGHTH) + *datum;
if ((i = hash_value & HIGH_BITS) != 0)
hash_value = (hash_value ^ (i >> THREE_QUARTERS)) & ~HIGH_BITS;
}
return (hash_value);
}
static int string_compare(void* a, void* b)
{
return (strcmp( (char*)a, (char*)b ) == 0);
}
/**
* Create a new hash table.
*
* @param[in] nbuckets -- number of buckets to create
* @param[in] hash_function -- pointer to the hashing function to be used
* @param[in] hash_key_compare -- pointer to the hash key comparison function to be used
*
* @returns pointer to new hash table.
*/
icl_hash_t *
icl_hash_create( int nbuckets, unsigned int (*hash_function)(void*), int (*hash_key_compare)(void*, void*) )
{
icl_hash_t *ht;
int i;
ht = (icl_hash_t*) malloc(sizeof(icl_hash_t));
if(!ht) return NULL;
ht->nentries = 0;
ht->buckets = (icl_entry_t**)malloc(nbuckets * sizeof(icl_entry_t*));
if(!ht->buckets) return NULL;
ht->nbuckets = nbuckets;
for(i=0;i<ht->nbuckets;i++)
ht->buckets[i] = NULL;
ht->hash_function = hash_function ? hash_function : hash_pjw;
ht->hash_key_compare = hash_key_compare ? hash_key_compare : string_compare;
return ht;
}
/**
* Search for an entry in a hash table.
*
* @param ht -- the hash table to be searched
* @param key -- the key of the item to search for
*
* @returns pointer to the data corresponding to the key.
* If the key was not found, returns NULL.
*/
void *
icl_hash_find(icl_hash_t *ht, void* key)
{
icl_entry_t* curr;
unsigned int hash_val;
if(!ht || !key) return NULL;
hash_val = (* ht->hash_function)(key) % ht->nbuckets;
for (curr=ht->buckets[hash_val]; curr != NULL; curr=curr->next)
if ( ht->hash_key_compare(curr->key, key))
return(curr->data);
return NULL;
}
/**
* Insert an item into the hash table.
*
* @param ht -- the hash table
* @param key -- the key of the new item
* @param data -- pointer to the new item's data
*
* @returns pointer to the new item. Returns NULL on error.
*/
icl_entry_t *
icl_hash_insert(icl_hash_t *ht, void* key, void *data)
{
icl_entry_t *curr;
unsigned int hash_val;
if(!ht || !key) return NULL;
hash_val = (* ht->hash_function)(key) % ht->nbuckets;
for (curr=ht->buckets[hash_val]; curr != NULL; curr=curr->next)
if ( ht->hash_key_compare(curr->key, key))
return(NULL); /* key already exists */
/* if key was not found */
curr = (icl_entry_t*)malloc(sizeof(icl_entry_t));
if(!curr) return NULL;
curr->key = key;
curr->data = data;
curr->next = ht->buckets[hash_val]; /* add at start */
ht->buckets[hash_val] = curr;
ht->nentries++;
return curr;
/**
* Free hash table structures (key and data are freed using functions).
*
* @param ht -- the hash table to be freed
* @param free_key -- pointer to function that frees the key
* @param free_data -- pointer to function that frees the data
*
* @returns 0 on success, -1 on failure.
*/
int
icl_hash_destroy(icl_hash_t *ht, void (*free_key)(void*), void (*free_data)(void*))
{
icl_entry_t *bucket, *curr, *next;
int i;
if(!ht) return -1;
for (i=0; i<ht->nbuckets; i++) {
bucket = ht->buckets[i];
for (curr=bucket; curr!=NULL; ) {
next=curr->next;
if (*free_key && curr->key) (*free_key)(curr->key);
if (*free_data && curr->data) (*free_data)(curr->data);
free(curr);
curr=next;
}
}
if(ht->buckets) free(ht->buckets);
if(ht) free(ht);
return 0;
}
int insertMsg(char* client_nickname,message_t *msg){
char* buf=malloc(msg->data.hdr.len);
strncpy(buf,msg->data.buf,msg->data.hdr.len);
hash_elem *user = icl_hash_find(hash, client_nickname);
if(user->nmsg == 32){
return -1;
}
msg_list *mex=malloc(sizeof(msg_list));
if(mex==NULL){
return -1;
}
mex->msg=malloc(sizeof(message_t));
if(mex->msg==NULL){
return -1;
}
mex->msg->data.buf=NULL;
setHeader(&mex->msg->hdr,2,msg->hdr.sender);
setData(&mex->msg->data,msg->data.hdr.receiver,buf,msg->data.hdr.len);
mex->next=NULL;
if(user->msg_head==NULL){
user->msg_head=mex;
user->msg_corr=user->msg_head;
}else{
user->msg_corr->next=mex;
user->msg_corr=user->msg_corr->next;
}
user->nmsg++;
return 0;
}
void freeHashData(void* data){
hash_elem* data2=(hash_elem*) data;
msg_list* tmp=data2->msg_head;
while(tmp!=NULL){
free(tmp->msg->data.buf);
free(tmp->msg);
data2->msg_head=data2->msg_head->next;
tmp=data2->msg_head;
}
//free(data2);
free(data);
free(tmp);
}
int main(int argc, char *argv[]) {
//tabelle hash
hash = icl_hash_create(1024, &hash_pjw, NULL); //rivedere 1024 forse troppo
//fai insert per registrazioni
hash_elem *user1 = malloc(sizeof(hash_elem));
user1->nickname=malloc(33);
strncpy(user1->nickname,"user1",33);
user1->nmsg=0;
user1->msg_head=NULL;
user1->msg_corr=NULL;
hash_elem *user2 = malloc(sizeof(hash_elem));
user2->nickname=malloc(33);
strncpy(user2->nickname,"user2",33);
user2->nmsg=0;
user2->msg_head=NULL;
user2->msg_corr=NULL;
icl_hash_insert(hash, "user1", (void*)user1);
icl_hash_insert(hash, "user2", (void*)user2);
message_t msg;
memset(&msg,0,sizeof(message_t));
msg.data.buf=NULL;
message_t msg2;
memset(&msg2,0,sizeof(message_t));
msg2.data.buf=NULL;
setHeader(&msg.hdr,1,"SERVER");
setHeader(&msg2.hdr,1,"SERVER");
setData(&msg.data,"user2","hello",strlen("hello")+1);
setData(&msg2.data,"user2","hello",strlen("hello")+1);
insertMsg("user2",&msg);
insertMsg("user2",&msg2);
icl_hash_destroy(hash, NULL, freeHashData);
return 0;
}
你在freeHashData中不够自由,可以是:
void freeHashData(void* data){
hash_elem* data2=(hash_elem*) data;
msg_list* tmp=data2->msg_head;
while (tmp != NULL) {
msg_list* next = tmp->next;
free(tmp->msg->data.buf);
free(tmp->msg);
free(tmp); /* you missed that */
tmp = next;
}
free(data2->nickname); /* you missed that */
free(data2);
}