Re: allocating mem using glib

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

 



a few things of note:
  1. you're printing the elements after you free the memory, that's not gonna work

  2. use (iterator = g_slist_next(iterator)) to increment; do not refer to (iterator->next) directly

  3. after you've freed the list data elements, you need to free the list itself as well:
    g_slist_free(items_list1);

  4. and then reset the list pointer to NULL:
    items_list1 = NULL

  5. 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.

  6. 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


  1.  // Clean up
  2.         for(iterator = items_list1; iterator != NULL; iterator=iterator->next)
  3.         {
  4.                 free(((item_data*)iterator->data)->item_name);
  5.                 g_free(iterator->data);
  6.         }
  7.  
  8.         for(iterator = items_list1; iterator != NULL; iterator=iterator->next)
  9.                 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

[Index of Archives]     [Touch Screen Library]     [GIMP Users]     [Gnome]     [KDE]     [Yosemite News]     [Steve's Art]

  Powered by Linux