Re: GList empty after iteration?



Le 12/09/2011 21:10, Craig a écrit :
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
below-->

      GtkTreeIter tree_iter;
      events = g_list_reverse(events);
      events = g_list_first(events);
      while(events)
      {
              gtk_list_store_append (list_store, &tree_iter);
              struct midi_event *me = events->data;
              gtk_list_store_set(list_store, &tree_iter,
                                 0, me->time_stamp,
                                 1, me->event_type,
                                 2, me->message1,
                                 3, me->message2,
                                -1);
              events = g_list_next(events);           

You do this until events stop matching the loop's condition "(events)",
so until it becomes NULL (e.g. there are no more next).  So when you use
events below, it's NULL, and NULL is a valid empty list.

Better user a temporary iter like

        GList *iter;

        for (iter = list; iter; iter = iter->next) {
                /* here use iter->data */
        }

Cheers,
Colomban

      }
      /// this is where the list appears to be empty!!!!
      events = g_list_first(events);
      while(events)
      {
              g_print("midi event \n");
              events = g_list_next(events);           
      }



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