Find the answer to your Linux question:
Results 1 to 3 of 3
Hello I am using g_hash_tables and am having some serious memory leaks. Everytime i call the g_hash_table_foreach function a list gets g_malloc'ed by ghash and this makes my memory grow ...
  1. #1
    Just Joined!
    Join Date
    Jan 2008
    Posts
    22

    Free memory allocated by g_malloc()

    Hello

    I am using g_hash_tables and am having some serious memory leaks. Everytime i call the g_hash_table_foreach function a list gets g_malloc'ed by ghash and this makes my memory grow a great deal.

    I need for a way to free this list.

    Code:
    // Returns a list of all the keys and the values
    static void get_record_foreach(gpointer key,gpointer value, gpointer data)
    {
        GList **values = data;
    
        *values = g_list_prepend(*values,value);
        *values = g_list_prepend(*values,key);
    }
    
    /* As of GLib-2.14, GLib has g_hash_table_get_values()
     */
    static GList *hash_table_get_records(GHashTable *hash)
    {
        GList *values = NULL;
    
        g_hash_table_foreach(hash, get_record_foreach, &values);
    
        return values;
    }
    This returns a memory allocated list. Which makes my app very mem intensive unless it is released.

    thanks

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I've never used any of this stuff, but google is your friend, so I googled.

    g_hash_table_foreach() is not the problem, of course. The problem is the list that is built by repeated calls to g_list_prepend().

    When you've come back from your call to g_hash_table_foreach, use the list you've built any way you want; it will occupy memory as long as you need it. When you're done with it, call g_list_free(), and your memory leak problems should be vastly reduced.

    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined!
    Join Date
    Jan 2008
    Posts
    22
    Thanks a lot.
    That is exactly what I did and my memory problems did go down a great deal.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...