Question

In my application, I have a GtkIconView with single selection mode. I want to get the text of the selected item (and act upon that further down the line). Right now, I have some code that I thin will work, however the actual getting of the GtkTreePath crashes the program (segfault).

Program received signal SIGSEGV, Segmentation fault.
0x0804d350 in minkovsky_ufilter_ufilter_window_on_filter_selected (
    e=0x8172010, self=0x8354d80)
    at /home/filip/ufilter/src/UfilterWindow.vala:281
281               TreePath item = filterchooser.get_selected_items().data;

Can you please tell me why it's happening (and how to fix it)?

Thanks.

Edit: I made some more tests, but they are still inconclusive. Namely, I wanted to check if get_selected_items() returned null (causing the problem), but it just segfaulted again in the new place. This is the code:

    [CCode (instance_pos = -1)]
    public void on_filter_selected (Event e) {

        if(filterchooser.get_selected_items() == null){ // <-- Now happens here
            stderr.printf("Null!");
            return;
        }

        TreePath item = filterchooser.get_selected_items().data; // <-- Used to happen here
        TreeIter iter;
        string selection;
        filterchooser_liststore.get_iter(out iter, item);
        filterchooser_liststore.get(iter, 0, out selection);
        stderr.printf("Selected filter: %s\n", selection);
    }

Edit 2: Here's the stripped-down version. It contains: Two source files, one glade file, and sample images, and a readme file with the compile command. You will need Valac v0.16, GTK+ 3.0 and GModule 2.0 (I could have stripped it further down, but it would just be missing the point: these will be needed in production code). Still segfaults.

Was it helpful?

Solution

The problem is that your signature for on_filter_selected doesn't match the signal it is connected to (GtkIconView::item-activated, or Gtk.IconView.item_activated if you prefer Valadoc). You have

public void on_filter_selected (Event e) ...

but it should be something like this

public void on_filter_selected (Gtk.IconView view, Gtk.TreePath path) {
  Gtk.TreeIter iter;
  string selection;

  GLib.return_if_fail (filterchooser_liststore.get_iter (out iter, path));
  filterchooser_liststore.get (iter, 1, out selection);
  GLib.stderr.printf ("Selected filter: %s\n", selection);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top