g_slist_free_full() only arrived in v2.28, so when the local glib satisfies this minimum version, then this would be the way to go.
r-
On Sun, Sep 2, 2012 at 11:02 AM, Jasper St. Pierre <jstpierre@xxxxxxxxxxx> wrote:
On Sun, Sep 2, 2012 at 5:00 AM, richard boaz <ivor.boaz@xxxxxxxxx> wrote:This is the only one I don't agree with. Why?
> a few things of note:
>
> you're printing the elements after you free the memory, that's not gonna
> work
>
> use (iterator = g_slist_next(iterator)) to increment; do not refer to
> (iterator->next) directly
Note that if you want to free list items at the same time, you should
> after you've freed the list data elements, you need to free the list itself
> as well:
> g_slist_free(items_list1);
use g_slist_free_full, which allows you to pass a function pointer to
free items with.
--
> and then reset the list pointer to NULL:
> items_list1 = NULL
>
> if your list is large, use g_slist_prepend() to add to the list, and then
> g_slist_reverse() after it's been filled. This is much quicker.
>
> convert all your str() functions to glib equivalents: g_strdup(),
> g_strdup_printf(), g_strconcat(), etc... This way, you can create your
> item_name data element in one statement (making it much easier to read and
> understand), instead of all the hoops you're jumping through at the moment.
>
> richard
>
>
> // Clean up
> for(iterator = items_list1; iterator != NULL;
> iterator=iterator->next)
> {
> free(((item_data*)iterator->data)->item_name);
> g_free(iterator->data);
> }
>
> for(iterator = items_list1; iterator != NULL;
> iterator=iterator->next)
> print_item((item_data*)iterator->data);
>
>
> On Sun, Sep 2, 2012 at 9:44 AM, Mostafa Alshrief
> <mostafa_alshrief@xxxxxxxxx> wrote:
>>
>> hi there,
>>
>> i have a question about allocating/deallocating memory using glib
>> i have created this simple app to demonstrate :
>> http://pastebin.com/kVncSgxh
>> the app creates a simple GSList list and fill it with a data structure of
>> type item_data
>>
>> at line 65 i use a loop to free mem allocated by malloc() and by g_new();
>> i really need to know if i'm allocating/deallocating memory in the correct
>> way
>>
>> _______________________________________________
>> gtk-list mailing list
>> gtk-list@xxxxxxxxx
>> https://mail.gnome.org/mailman/listinfo/gtk-list
>
>
>
> _______________________________________________
> gtk-list mailing list
> gtk-list@xxxxxxxxx
> https://mail.gnome.org/mailman/listinfo/gtk-list
>
Jasper
_______________________________________________ gtk-list mailing list gtk-list@xxxxxxxxx https://mail.gnome.org/mailman/listinfo/gtk-list