0

I was trying to load a Glade layout into a GTK2 C code.

It consists of a window in which there is a VBox with 2 elements: a label and a button. I want to associate a signal to the button to close the application.

If I make all of this by using only C code it works fine, but when I load a layout I receive the following runtime error (the application starts but the button doesn't work):

(main:2581): GLib-GObject-WARNING **: 21:38:46.789: invalid (NULL) pointer instance (main:2581): GLib-GObject-CRITICAL **: 21:38:46.790: g_signal_connect_data: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed 

Here the code I used, mylayout.glade is a file in my project folder:

#include <gtk/gtk.h> void end_program (GtkWidget * wid, gpointer ptr) { gtk_main_quit(); return; } int main (int argc, char * argv[]) { GError * error = NULL; gtk_init(&argc, &argv); GtkBuilder * builder = gtk_builder_new(); if (0 == gtk_builder_add_from_file(builder, "mylayout.glade", &error)) { g_printerr("Error loading file: %s\n", error->message); g_clear_error(&error); return 1; } GtkWidget * win = (GtkWidget *) gtk_builder_get_object(builder, "window1"); GtkWidget * btn = (GtkWidget *) gtk_builder_get_object(builder, "button1"); g_signal_connect(btn, "clicked", G_CALLBACK(end_program), NULL); gtk_widget_show_all(win); gtk_main(); return 0; } 

The only thing I noticed is that if I remove the g_signal_connect() line the error disappear.

Any help is really appreciated.

4
  • 1
    You don't check your return values for NULL. If that g_signal_connect call causes these messages, then btn is NULL. Commented May 6, 2022 at 19:57
  • Maybe showing your glade file might help. Commented May 6, 2022 at 20:20
  • Solved, GTK2 has errors that are different from the classic gcc compiler and I didn't know what was the error. Thank you @Gerhardh Commented May 8, 2022 at 19:28
  • I have no idea what you mean by "has errors that are different from the classic GCC compiler". To see the errors, you must check return values. A compiler can only report syntax errors. The vast majority of errors are runtime errors your compiler cannot tell you. Commented May 10, 2022 at 8:19

1 Answer 1

1

You might want to check the name you gave your button widget in the glade file. The only way I could replicate your error was when the name of the button widget defined in the glade file was something different from "button1" as referenced in the program statement:

GtkWidget * btn = (GtkWidget *) gtk_builder_get_object(builder, "button1"); 

When the glade file had a different name for the button (e.g. "button" or "btn"), then I received the identical warning and critical error message as you noted.

Regards.

Sign up to request clarification or add additional context in comments.

1 Comment

Yep, it's right! It is the first time I get these errors and now I know what's their origin. I can confirm that the real name of the button, in my case, was "button2".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.