Re: g_list_append help!



Brett Nash wrote:

I am developing an interactive GUI database using Glade and getting stuck with glist. Anybody knows how to supply parameter to g_list_append by a string variable instead
of a physical string?  In particular, instead of the following line:

combo2_items = g_list_append (combo2_items, (gpointer) _("blue"));

I want to use:
combo2_items = g_list_append (combo2_items, (gpointer) _(entry_name));

where entry_name is a string variable. I tried this and what appeared in combo2_items was not the string represented by entry_name, but some weird characters???

Firstly you don't want to translate it:
       c2items = g_list_append(c2items, entry_name);

Otherwise what is the scope of entry name?  Is a malloced array?  Is it
being reused?  My best guess would be that entry_name is allocated on
the stack and you returning from the function meaning the value of the
string is beign trashed.

To test this try strduping what you add to the list (watch for future
memory leaks):
       c2items = g_list_append(c2items, g_strdup(entry_name));

Note moving from a (char *) to a (void */gpointer) doesn't really need a
cast (unless a gpointer is not a void * on some platform).

       nash
Dont forget, if you g_strdup() a string, you need to make sure that you remove it before you free it, e.g:

 gchar *mystring = (gchar*) g_list_nth_data(mylist, index);
 mylist = g_list_remove(mylist, my_string);
 if(mystring != NULL) g_free(mystring);

Also, if you explicitly append something, e.g.

 mylist = g_list_append(mylist, "mytext");

You dont need to create the memory for it and you CERTAINLY dont need to free it.

Regards,
Martyn




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