title: GTK in C: Segmentation Fault when using key-release or key-press
body: <p>For context, I'm trying to make a program that changes stylized label text in response to some physical inputs on a beaglebone black (eg. get a signal, if high/low, show this text). In lieu of those inputs, which I don't have access to right now, I decided to use key-release as a substitute.</p>
  
  <p>I've been basing my code off of this <a href="http://zetcode.com/gui/gtk2/gtkwidgetsII/" rel="nofollow">combo-box tutorial</a>, which changes label text based on the text of a combo-box selection. I've modified that code to use stylized text as in the code below.</p>
  
  <pre><code>#include &lt;gtk/gtk.h&gt;
  
  void combo_selected(GtkWidget *widget, gpointer window) {
  
    gchar *text = g_strjoin(NULL,"&lt;span font='48' weight='bold' color='#DDDDDD'&gt;",gtk_combo_box_get_active_text(GTK_COMBO_BOX(widget)),"&lt;/span&gt;",NULL); //label text, uses pango markup
    gtk_label_set_markup(GTK_LABEL(window), text);
    g_free(text);
  }
  
  int main(int argc, char *argv[]) {
  
    GtkWidget *window;
    GtkWidget *hbox;
    GtkWidget *vbox;
    GtkWidget *combo;
    GtkWidget *label;
  
    gtk_init(&amp;argc, &amp;argv);
  
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "GtkComboBox");
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    gtk_container_set_border_width(GTK_CONTAINER(window), 15);
    gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
  
    hbox = gtk_hbox_new(FALSE, 0);
    vbox = gtk_vbox_new(FALSE, 15);
  
    combo = gtk_combo_box_new_text();
    gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "Ubuntu");
    gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "Arch");
    gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "Fedora");
    gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "Mint");
    gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "Gentoo");
    gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "Debian");
  
    gtk_box_pack_start(GTK_BOX(vbox), combo, FALSE, FALSE, 0);
  
    gchar *str = "&lt;span font='48' weight='bold' color='#DDDDDD'&gt;Not Initialized&lt;/span&gt;"; //label text, uses pango markup
    label = gtk_label_new(NULL);
    gtk_label_set_markup(GTK_LABEL(label), str); //add pango str to label
  
    gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
  
    gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
    gtk_container_add(GTK_CONTAINER(window), hbox);
  
    g_signal_connect(G_OBJECT(window), "destroy",
          G_CALLBACK(gtk_main_quit), NULL);
  
    g_signal_connect(G_OBJECT(combo), "changed", 
          G_CALLBACK(combo_selected), (gpointer) label);
  
    gtk_widget_show_all(window);
  
    gtk_main();
  
    return 0;
  }
  </code></pre>
  
  <p>That works, however when attempting to use a key-release or key-press instead of the combo box options, I get a segmentation fault. Below is the further-modified code (with some things commented out).</p>
  
  <pre><code>#include &lt;gtk/gtk.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  
  void fpcheck(GtkWidget *window, gpointer lbl) {
    gchar *text = "&lt;span font='48' weight='bold' color='#DDDDDD'&gt;Press index finger firmly on sensor.&lt;/span&gt;";
    gtk_label_set_markup(GTK_LABEL(lbl), text);
  
    //sleep(2); //placeholder -&gt; fp detection
  
    //text = "&lt;span font='48' weight='bold' color='#DDDDDD'&gt;Fingerprint recognized!&lt;/span&gt;";
    //gtk_label_set_markup(GTK_LABEL(lbl), text);
    g_free(text);
  }
  
  int main(int argc, char *argv[]) {
  
    GtkWidget *window; //main window
    GtkWidget *align; //alignment settings
    GtkWidget *lbl; //text
    GdkColor color = {0, 0x0, 0x0, 0x0}; //window color
  
    gtk_init(&amp;argc, &amp;argv);
  
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL); //init window
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); //window pos on screen
    gtk_window_set_default_size(GTK_WINDOW(window), 800, 480); //window size
    //gtk_window_set_resizable(GTK_WINDOW(window), FALSE); //user cant resize
    gtk_window_set_title(GTK_WINDOW(window),"User Display"); //window title
    gtk_widget_modify_bg(window, GTK_STATE_NORMAL, &amp;color); //set color to window
    align = gtk_alignment_new(.5,.5,0,0); //x,y alignment
    lbl = gtk_label_new(NULL); //label init
  
    gchar *str = "&lt;span font='48' weight='bold' color='#DDDDDD'&gt;Not Initialized&lt;/span&gt;"; //label text, uses pango markup
    gtk_container_add(GTK_CONTAINER(align), lbl); //add label to alignment
    gtk_container_add(GTK_CONTAINER(window), align); //add label to window
    gtk_label_set_markup(GTK_LABEL(lbl), str); //add pango str to label
  
    g_signal_connect(G_OBJECT(window), "key-release-event", G_CALLBACK(fpcheck), (gpointer) lbl); //calls fpcheck to change label     
  
    g_signal_connect(G_OBJECT(window), "destroy",
          G_CALLBACK(gtk_main_quit), NULL);
  
    gtk_widget_show_all(window); //build the window all at once
  
    gtk_main();
  
    return 0;
  }
  </code></pre>
  
  <p>If I comment out the <code>gtk_label_set_markup</code> line and <code>g_free(text)</code> line in <code>fpcheck</code>, there's no error but it doesn't do anything, of course. From looking at other online resources, I think this error is being cause by trying to access the <code>GTK_LABEL(lbl)</code> because of the first argument in <code>fpcheck</code> being incorrect, but thats just a guess and I don't know what I'd put there instead. In the example, it's just "Widget," referring to the combo-box, I think, since it gets the text from the selected option. </p>
  
  <p>On that note, I tried removing that argument, and now instead of a segmentation error, I get the following error without even pressing/releasing.</p>
  
  <pre><code>(test:6698): GLib-GObject-WARNING **: invalid cast from 'GtkWindow' to 'GtkLabel'
  
  (test:6698): Gtk-CRITICAL **: IA__gtk_label_set_markup: assertion 'GTK_IS_LABEL (label)' failed
  </code></pre>
  
  <p>I did a few gdb backtraces, but none of them seem to be particularly helpful. If anyone wants 'em, I can post them.</p>
  
  <p>Any ideas on the problems I'm having?
  Thanks.</p>