tags:

views:

35

answers:

3

Hello,

I need to remove all and clear GList in my gtk+ application. How can i do this?

Thank you

A: 

You have to use the API to free the list and update the container containing the List in your UI application appropriately.

void g_list_free (GList *list);

This way the memory allocated for creating the list is freed and the UI container is refreshed to display a blank list.

Praveen S
`g_list_free` doesn't refresh any UI container.
ptomato
@ptomato: I have asked him to refresh the UI container in the first line.
Praveen S
A: 

The function g_list_free can do the job, but don't forget to free the data before if allocated dynamically, as also said here: in that case you have to go through each element of the list and use g_free (if allocated with g_malloc), or free (if allocated with malloc), or whatever matches the alloc function (e.g delete for new in C++ ...)

ShinTakezou
+1  A: 

An easy way to free the list and the data in it, and then clear the list, goes like this:

g_list_foreach(list, g_free, NULL);
g_list_free(list);
list = NULL;

NULL is the empty list, so that last line clears it so that you can use it again.

Of course if your data should be freed by some other function, use that function instead of g_free() as ShinTakezou remarks.

ptomato