1

I have two structures like so:

struct item2_map{
char stringkey[MAX_SIZE];
UT_hash_handle hh;
}

struct item1_map{
int key1;
struct item2_map *item2_map;
UT_hash_handle hh;
}

In my code I do something like this

struct item1_map *retrieved;
struct item2_map *found_value, *tmp;

HASH_FIND(hh, hash_head, key1, &someintvalue, sizeof(int), retrieved)
if(retrieved==NULL)
{
    HASH_ADD(hh, hash_head, key1, sizeof(key1), my_item1);
    my_item1->item2_map = NULL;
    HASH_ADD_STR(my_item1->item2_map, stringkey, my_item2);
} else 
{
    //THIS WORKS
    HASH_ITER(hh, retrieved->item2_map, found_value, tmp)
    { //do something }

    //THIS SEG FAULTS
    HASH_FIND_STR(retrieved->item2_map, &my_item2->stringkey, found_value)
}

This seems to give me a seg fault on HASH_FIND_STR(). Is there something I am doing wrong? In this example, assume that my_item1 and my_item2 came from somewhere else and are valid. I want to use stringkey as the key to find the values.

I put a breakpoint in the IF part of the conditional, so i Know that it is not found at first, then on the second find of that key, the else block is entered.

Interestingly enough, if I use HASH_ITER to iterate over the entries, it seems to "work" at least without crashing, though im not convinced all the values are the same.

Derek
  • 11,715
  • 32
  • 127
  • 228

1 Answers1

3

It looks basically right. A few things to double check

  • check that my_item1->item2_map is initialized to NULL when my_item1 is created, before the HASH_ADD_STR
  • check that my_item2->stringkey is null-terminated and less than MAX_SIZE before HASH_FIND_STR
  • try running it under valgrind to see if there are any other clues

Let us know if you find anything.

Troy

  • So as it turns out I figured out what the problem was. In my code, the my_item2 was being populated by just reusing the same structure instead of allocating a new one every time. This gave me a different value for the keys, but I ended up needing to do a memcpy to a new structure before inserting it in the hash and that seems to have solved the problem. – Derek Apr 10 '13 at 22:02