Re: GList weirdness





The "row" variable is being reused and does not contain a copy. You need to
do something like this:

   gpointer tmp;

   while((row = mysql_fetch_row(result))) {
        printf("fetching %s\n", row[0]);
        tmp = NULL;
        if (row[0] != NULL)
          tmp = (gpointer) strdup(row[0]);
        countries = g_list_append(countries, tmp);
        printf("countries = %s\n", countries->data);

   }


It's actually not being reused. Each Successive call to mysql_fetch_row() returns a new value, and only returns a NULL value when there are no more rows to fetch. For the sake of other suggestions, I have included the whole file below. (Don't worry, it's short)

#include <mysql/mysql.h>
#include <gtk/gtk.h>
#include <stdio.h>

GtkWidget *populate_country_dropdown()
{
 GtkWidget *combo;
 GList *countries = NULL;
 MYSQL mysql;
 MYSQL_RES *result;
 MYSQL_ROW row;
 int error;

 printf("starting country dropdown thingy...\n");

 if(!mysql_init(&mysql))
   {
     g_warning("omg! init failed!\n");
   }

if(!mysql_real_connect(&mysql, "[host]", "[user]", "[password]", "[database]", 0, "/tmp/mysql.sock", 0))
   {
     g_warning("doh!  connect failed...\n");
     exit(1);
   }
 else
   {
     error = mysql_select_db(&mysql, "label");
     error = mysql_query(&mysql, "SELECT country_name FROM _countries");
     result = mysql_use_result(&mysql);

     while((row = mysql_fetch_row(result)))
	{
	  g_message("fetching %s", row[0]);
	  countries = g_list_append(countries, row[0]);
	  g_message("countries = %s", countries->data);

	}
     g_message("outside countries = %s", countries->data);
     combo = gtk_combo_new();
     gtk_combo_set_popdown_strings(GTK_COMBO(combo), countries);

     mysql_free_result(result);

     return(combo);
   }
}


The output is as follows:

starting country dropdown thingy...
Message: fetching United States
Message: countries = United States
Message: fetching Canada
Message: countries = Canada
Message: outside countries =


Per Sven's suggestions, I replaced standard C printf's with the glib equivalents. Turns out it was not abuffer problem. I also tried using g_list_prepend() instead of g_list_append, but the results are still the same. Any other ideas/suggestions would be greatly appreciated.

Also...as you can see, this func is creating a combo box. The box itself DOES show up, but it has two blank entries, and nothing else.

Thanks...

-HaB



_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp





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