Question

I am trying to make a gtk application using gtk3 that uses data from a mysql database.

Here is the code that has a problem

GType* types = (GType*) malloc(num_fields * sizeof(G_TYPE_STRING));
    for(i = 0; i < num_fields; i++) {
        types[i] = G_TYPE_STRING;
    }
    store = gtk_list_store_newv(num_fields, types);

    b=0;
    while ((row = mysql_fetch_row(result)))
    {
        if (b==0) b=1;
        else gtk_list_store_append (store, &iter1);
        for(i = 0; i < num_fields; i++)
        {
            if (i == 0) while((field = mysql_fetch_field(result))!=NULL) mdata[i]=field->name;
            else 
            {
                GValue val = G_VALUE_INIT;
                g_value_init(&val, G_TYPE_STRING);
                g_value_set_string (&val,row[i]);
                gtk_list_store_set_value (store, &iter1,i-1,&val);

            }           
        }
    }

When I try to run the programm I see this error :

Gtk-CRITICAL **: gtk_list_store_set_value: assertion `iter_is_valid (iter, list_store)' failed

Does anybody have an idea how to fix the problem?

Because I am new to stackoverflow if there are some more data I need to post or the title is not good please comment.

Was it helpful?

Solution

The logic with b is super-strange. The first time you run the code, when b is 0, you won't call gtk_list_store_append(), so iter1 will be left non-initialized which causes the error.

Also as pointed out by Mr Pileborg, don't cast the return value of malloc(), and in a GTK+ program seriously consider using g_malloc() instead. Also the size argument is scary in your code, it should be:

GType* types = g_malloc(num_fields * sizeof *types);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top