Re: allocating mem using glib
- From: richard boaz <ivor boaz gmail com>
- To: Mostafa Alshrief <mostafa_alshrief yahoo com>
- Cc: "gtk-list gnome org" <gtk-list gnome org>
- Subject: Re: allocating mem using glib
- Date: Sun, 2 Sep 2012 11:00:09 +0200
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
- after you've freed the list data elements, you need to free the list itself as well:
g_slist_free(items_list1);
- 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 yahoo com> 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 gnome org
https://mail.gnome.org/mailman/listinfo/gtk-list
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]