HPerin
HPerin

Reputation: 59

GtkListStore adding (segmentation fault)

I am trying to pass a GSList to a GtkListStore to show to the user. But when i set the value to the GtkListStore, it just gives me segmentation fault. I really dont know where is the problem. I tested the list, so it is not empty, and it have gchar vectors, as expected.

enum {
    COLUMN_FILENAME,
    NUM_COLUMNS
};

GtkBuilder *builder;

void add_to_source_list(gpointer filename, gpointer list_store) {
    GtkTreeIter iterator;
    gtk_list_store_append(GTK_LIST_STORE(list_store), &iterator);

    // When the program reaches this line it gives segmentation fault
    gtk_list_store_set_value(list_store, &iterator, COLUMN_FILENAME, filename);
}

void source_files_list_update() {
    GSList *list = file_manager_get_all_sources();

    GtkListStore *list_store;
    list_store = gtk_list_store_new(NUM_COLUMNS,
                                G_TYPE_STRING);

    GtkTreeView *tree_view;
    tree_view = GTK_TREE_VIEW(gtk_builder_get_object(builder, TREE_VIEW_ID));
    g_assert_true(tree_view != NULL);
    gtk_tree_view_set_model(tree_view, GTK_TREE_MODEL(list_store));

    GtkCellRenderer *renderer;
    renderer = gtk_cell_renderer_text_new();

    GtkTreeViewColumn *column;
    column = gtk_tree_view_column_new_with_attributes("FILENAME", renderer, "text", COLUMN_FILENAME, NULL);

    gtk_tree_view_append_column(tree_view, column);
    g_slist_foreach(list, add_to_source_list, list_store);
}

Upvotes: 0

Views: 312

Answers (2)

andlabs
andlabs

Reputation: 11598

gtk_list_store_set_value() takes a GValue as its last argument, not a string. You would normally get a compiler warning for situations like this, but in this case filename is still a gpointer, which is a void *, and thus the compiler assumes you were doing the right thing.

The easiest way to fix your program is to use gtk_list_store_set() instead. Read the documentation (and follow jcoppens's advice anyway, though I'm not sure if it would have helped in this case...).

Upvotes: 2

jcoppens
jcoppens

Reputation: 5440

I suggest you run the code in gdb. You didn't say which IDE (if any) you are using, but you may have to recompile the program with the -g -O0 options to gcc:

gcc -g -O0 <old options>

(The -O0 switches off any optimization, so following the code is simpler. -g includes debug info).

Then you can run the program in gdb, and, after the segmentation fault, do a 'backtrace' with the command bt. This will give you the exact function where the segfault occurred and make it easier to pinpoint the problem.

I'd also try adding a printf("%s\n", filename); just where you put the comment about the segfault. Then you can see when the segfault occurs (if there were successful passes or not)

Upvotes: 0

Related Questions