Hash Tables



Hi!
I currently ues GHashTables and I want to destroy them at the end of the run.
Here follows the code I use to create an fill the tables:

int
crea_hash_ue()
{
_UE *punt;  /* UE is a structure that I've defined*/
gchar **key;
int i,indice=0;

TABELLA_UE=g_hash_table_new(g_str_hash,g_str_equal);

nomi_ue=(gchar**)malloc((size_t)(4*sizeof(gchar*)));
numero_ue=4;

punt=(_UE*)malloc((size_t)sizeof(_UE));
key=(gchar**)malloc((size_t) sizeof(gchar*));

nomi_ue[indice]=(gchar*)malloc((size_t)(1024*sizeof(gchar)));
punt[indice].class=1;
punt[indice].tx_min=-44;
punt[indice].tx_max=33;
punt[indice].a_gain=0;
punt[indice].noise_figure=9;
punt[indice].riga=indice;
punt[indice].disalloca=0;
key[indice]=g_strdup_printf("%d",indice);
nomi_ue[indice]=g_strdup_printf("%d",punt[indice].riga);


punt_ue=punt; /* 2 global variables that I suppose useful for free the allocated memory */
chiave_ue=*key;

punt=(_UE*)realloc(punt,(++indice+1)*sizeof(_UE));
key=(gchar**)realloc(key,(indice+1)*sizeof(gchar*));
nomi_ue[indice]=(gchar*)malloc((size_t)(1024*sizeof(gchar)));
punt[indice].class=2;
punt[indice].tx_min=-44;
punt[indice].tx_max=27;
punt[indice].a_gain=0;
punt[indice].noise_figure=9;
punt[indice].riga=indice;
punt[indice].disalloca=0;
key[indice]=g_strdup_printf("%d",indice);
nomi_ue[indice]=g_strdup_printf("%d",punt[indice].riga);



indice++;

for (i=0;i<indice;i++){
  g_hash_table_insert(TABELLA_UE,(gpointer)key[i],&punt[i]);
}

return(indice);
}

After using the table I try to free the memory allocated with malloc and realloc and to destroy the table as follow:

g_hash_table_foreach(TABELLA_SERVIZI,(GHFunc) g_hash_table_remove,NULL);
free(punt_ue);
free(chiave_ue)
g_hash_table_destroy(TABELLA_SERVIZI);

and also in this way:

free(punt_ue);
free(chiave_ue)
g_hash_table_destroy(TABELLA_SERVIZI);

In both cases the result is segmentation fault!!!
How can I do?
Thank-you!
Chiara













[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]