Re: Glib: g_tree_foreach and GTransverseFunc questions.



Le vendredi 19 ao�05 �2:22 -0400, Lucas Brasilino a �it :
> Hi!
> 
> I'm writing a little daemon which I'm storing
> user login and IP address as key and value pair
> respectively, which I malloc()ed buffers for both,
> using Balanced Binary Tree interface
> from Glib 2.6.4. I'm using FC4. Eventually I compile
> it with Glib 2.4.8 in a FC3 box.
> 
> I'm creating a tree with g_tree_new() and destroying
> it with g_tree_destroy(). But before destroying it
> I'm tranversing the entire tree with g_tree_foreach()
> and free()ing both key and value.
> 
> It seems to work fine, since valgrind tells there's
> no memory leak.
> But I have no idea what user_data argument is used for
> within g_tree_foreach() neither data argument of
> GTransverseFunc. Any explanation ?

The user_data argument is an extra argument you can use for your
purpose. If you don't need any, passing NULL is OK.

> I've made user_data as NULL and did't touch
> GTransverseFunc's data argument:
> 
> g_tree_foreach (SIConfig->tree, freeKeyData, NULL);
> 
> gboolean
> freeKeyData (gpointer key, gpointer value, gpointer data)
> {
>    free (key);
>    free (data);
> 
>    return FALSE;
> }

Does this really work? you should free value, not data.
> It works.. :) but...

Instead of doing this, you can create your tree with g_tree_new_full and
use (GDestroyNotify)g_free as third and fourth arguments. Using g_free
is better than using free.

-- 
Jean Br�rt <jean brefort normalesup org>





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