Re: GList empty after iteration?



On 2011-09-12 15:10, Craig <craigbakalian verizon net> wrote:
Hi All,

I am confused about GList. My application is parsing a file and creating
midi data with it. I am using GList to store the midi data.  I have
created a GtkListStore to view the parsed data (to insure I am parsing
the data properly).  So, below I iterate through the GList and "copy"
the data into a tree view.  But, I am shocked to see that after I
iterate through the GList, I cannot iterate through the list again.  I
have debugged the list after the iteration with g_list_length(events)
which shows the list length at 0.  What is up with this?  The first
while loop has data, the second while loop has not data. The code is

Another option to those offered in this thread is to use the handy g_list_foreach() to iterate over the list.

http://developer.gnome.org/glib/stable/glib-Doubly-Linked-Lists.html#g-list-foreach

This nicely separates out the loop into a function, something like (I have not tested this, and it can probably be simplified):

void my_list_foreach_store (gpointer data, gpointer user_data)
{
    GtkTreeIter tree_iter;
    struct midi_event *me = data;
    GtkListStore list_store = GTK_LIST_STORE (user_data);

    gtk_list_store_append (list_store, &tree_iter);
    gtk_list_store_set (list_store, &tree_iter,
                        0, me->time_stamp,
                        1, me->event_type,
                        2, me->message1,
                        3, me->message2,
                       -1);
}

void my_list_foreach_print (gpointer data, gpointer user_data)
{
    g_print ("midi event \n");
}

void a_function ()
{
     GList *events = NULL;
     GtkListStore *store = gtk_list_store_new();

     events = g_list_prepend (events, some_data);
     /* insert more items */

     events = g_list_reverse (events);
/* No need for g_list_first (), already handled by * g_list_reverse ().

     g_list_foreach (events, my_list_foreach_store, store);
     g_list_foreach (events, my_list_foreach_print, NULL);
}

--
http://amigadave.com/



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