GHashTable Frustration



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.

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.

ANY help on this would be very appreciated.  I've begun to think I'm
just slow or something.

Thank you,
Les




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