Re: GHashTable Frustration



On Thu, 2005-12-15 at 02:55 -0800, Les Harris wrote:
Hi all,

I am going absolutely crazy.  I am trying to use a GHashTable with
string keys to pull out a struct value.   The first time the code runs
it works, but any subsequent times the code will segfault.  I really am
at a loss and have found nothing in the documentation or through google
that helps me.

At first I thought it was some memory fubar in my application so I
started coding a bare minimum 'GHashTable' thing and I still get the
problem.

You should post here your TriggerDestroy function, as this code working
for the first run could mean the destroy function is bogus.

The first g_hash_table_replace adds the data, the second
g_hash_table_replace deletes the old one and fails.

It could be also the TrigerEqual function, because before to replace the
data, this function is used to look for the replacement data.

Did you tried to debug it ?

ITOH to call g_type_init() before to use GTypes may help.

int main(void) {
      int i;
      Trigger *newTrigger;

      g_message("Creating hash table...");

      gTriggerHash = g_hash_table_new_full(g_str_hash, TriggerEqualFunc, 
                                    g_free, TriggerDestroy);
                                                                                                       
      g_message("Entering loop, going to replace some keys...");

      for(i = 0; i < 10; i++) {
              g_message("Iteration %d Start",i);
              
              g_message("Alloc'ing new Trigger");
              newTrigger = g_new(Trigger,1);
              
              g_message("Alloc'ing new string");
              newTrigger->action = g_new(gchar,255);
              
              g_message("Copying action string into struct...");
              strcpy(newTrigger->action,"Foobar!");
              g_message("Action string copied: %s", newTrigger->action);
              
              g_message("Setting enabled to TRUE in struct...");
              newTrigger->enabled = TRUE;
      

              /* This works the First time through
                   and only the first time through.... */
              g_message("Trying to add to hash table...");
              g_hash_table_replace(gTriggerHash,g_strdup("foobarkey"), newTrigger);
      
              g_message("Writing to disk...");
              writeTriggers();
      }
      
      g_message("Done!");
      return 0;
}
I suspect its something to do with the scope of newTrigger but since I'm
dynamically allocating it I don't see how.

May be the deletion of the old trigger ??

-- 
Iago Rubio




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