2

I am new to the GLIB library. g_hash_table_insert() seems to be overriding previously inserted values. When I make a print out of both keys and values of saved data, the keys are still unique and unchanged but the values are ALL the same. I am storing a struct type into the GHashtable and this is its structure:

struct _DsectionEntity {
  ...
  int entity_type;
  int sequence_number;
  ...
};

typedef struct _DsectionEntity DsectionEntity;

I parse an IGES model file line by line and create DsectionEntity object after parsing 2 lines of the D section of the IGES file. I use the sequence number of the object as a key and the entire object as the value. The code for creating the hash table and insertion of values is as follows:

void
get_dsection(IgesFile *fp, DsectionEntity *ds)
{
  char *line1 = malloc(91);
  char *line2 = malloc(91);

  /* dsection_ht GHashtable declared as a global var and initialized to NULL */
  dsection_ht = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);

  line1 = get_line(fp, line1);
  while (line1) {
    if (line1[72] == 'D') {
      line2 = get_line(fp, line2);

      /* create Object */
      parser_dsection_new(ds, line1, line2);

      /* insert Object into the hashtable */
      parser_add_ds_object(dsection_ht, ds);
      line1 = get_line(fp, line1);
    } else {
      line1 = get_line(fp, line1);
    }
  }
}

The code for inserting:

void
parser_add_ds_object(GHashTable * ht, DsectionEntity *dsec_entity)
{
  // printf("KEY : %d\n", GPOINTER_TO_INT(GINT_TO_POINTER(dsec_entity->sequence_number)));
  // printf("SQ : %d\n", dsec_entity->sequence_number);
  // printf("Entity : %d\n", dsec_entity->entity_type);
  // printf("\n");
  g_hash_table_insert(ht, GINT_TO_POINTER(dsec_entity->sequence_number), (gpointer)dsec_entity);
}

If comments are removed on printf, output(WHICH IS CORRECT) is:

enter image description here

void
print_values(gpointer key, gpointer value, gpointer userdata)
{
  int realkey = GPOINTER_TO_INT(key);
  DsectionEntity *realvalue = (DsectionEntity *)value;

  printf("KEY : %d\n", realkey);
  printf("SQ : %d\n", realvalue->sequence_number);
  printf("Entity : %d\n", realvalue->entity_type);
  printf("====================================\n");
}

If I display using g_hash_table_foreach(dsection_ht, print_values, NULL) and print_values() shown above. I get:

enter image description here

The object with the repeated sequence_number and entity_type is the last one added into the GHashtable (shown in image 1). Valgrind shows no error, so what could be the problem?

File Format is IGES (INITIAL GRAPHICS EXCHANGE SPECIFICATION). Code for parser_dsection_new():

void
parser_dsection_new(DsectionEntity *dsec_entity, char *line1, char *line2)
{
  char substr[10];

  get_field(line1, substr, 1, 8);
  dsec_entity->entity_type = utils_to_int(substr);

  // ...

  get_field(line1, substr, 74, 8);
  dsec_entity->sequence_number = utils_to_int(substr);

  get_field(line1, substr, 9, 8);
  dsec_entity->line_weight = utils_to_int(substr);

  get_field(line1, substr, 17, 8);
  dsec_entity->color = utils_to_int(substr);

  get_field(line1, substr, 57, 8);
  dsec_entity->entity_label = substr;
  // ...
}
10
  • @AnttiHaapala I don't understand what you mean by 2 objects.
    – Minathe
    Aug 23, 2018 at 13:44
  • @Minathe: Please update your file format along with the definition of parser_dsection_new function.
    – Azeem
    Aug 23, 2018 at 13:45
  • @Minathe: Can you check the return value of g_hash_table_insert whether it is TRUE or FALSE? You may need to move those prints inside the if to check e.g. if ( g_hash_table_insert(...) == TRUE ) { /* print values */ }.
    – Azeem
    Aug 23, 2018 at 14:27
  • @Azeem OUTPUT is LIKE IMAGE 1 i.stack.imgur.com/dmPB4.png All values are printed correctly.
    – Minathe
    Aug 23, 2018 at 14:46
  • 1
    SOLVED!!! Added ds = (DsectionEntity *) malloc(sizeof(DsectionEntity)); after parser_add_ds_object(dsection_ht, ds);
    – Minathe
    Aug 23, 2018 at 15:32

1 Answer 1

2

According to the reference of g_hash_table_insert():

Inserts a new key and value into a GHashTable.

If the key already exists in the GHashTable its current value is replaced with the new value. If you supplied a value_destroy_func when creating the GHashTable, the old value is freed using that function. If you supplied a key_destroy_func when creating the GHashTable, the passed key is freed using that function.

So, the g_hash_table_insert() API is supposed to replace the existing value with the new one.


But, your problem is that you are using only one instance of DsectionEntity for everything i.e. parsing, inserting, etc. whereas you ought to have a unique instance every time you want to insert a new key/value pair in the hash. In your code, the same instance is overwritten that's why you're seeing the latest value only. Use unique instances and it will work.

You can use default delete functions in g_hash_table_new_full() along with g_hash_table_remove_all() like this:

// Create hash table with default delete callbacks
ht = g_hash_table_new_full(g_direct_hash, g_direct_equal, g_free, g_free);
//                                                        ~~~~~~  ~~~~~~

// Allocate new entry
ds = (DsectionEntity*) malloc( sizeof(DsectionEntity) );

// Populate entry
// ...

// Insert entry in hash table
g_hash_table_insert( ht, 
                     GINT_TO_POINTER( ds->sequence_number ), 
                     (gpointer) ds);

// ^^^ Do check the return value of g_hash_table_insert

// Remove hash table at the end
g_hash_table_remove_all( ht );

Check this example for your guidance.


Alternative:

You may want to explore UT Hash as an alternative hash table solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.