Marco Zamboni
Marco Zamboni

Reputation: 224

GtkWidget* returned from a function lead to segmentation fault

I'm asking why if I decide to call a function in another file the returning GtkWidget* will cause a segmentation fault if used (a cast, a gtk function like in the example, ...)

Here in main I run gtk_init and try GTK_IS_WIDGET from the function defined in the other file

main.c

#include <gtk/gtk.h>
#include <glib.h>

int main(){
  gtk_init(NULL, NULL);
  GTK_IS_WIDGET(new_widget());
}

There in the other file I create a new GtkWidget and use the function GTK_IS_WIDGET, then I print an "OK" in order to know if the execution arrives at this point

extern_file.c

#include<gtk/gtk.h>
GtkWidget* new_widget(){
  GtkWidget *new = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  GTK_IS_WIDGET(new);
  g_print("OK\n");
  return new;
}

I compile the two file in this way gcc -o main.o main.c esterno.c $(pkg-config --cflags --libs glib-2.0 gtk+-2.0) I get many warnings, (Which I put down there) but no error.

main.c:6:17: warning: implicit declaration of function ‘new_widget’ [-Wimplicit-function-declaration]
   GTK_IS_WIDGET(new_widget());
                 ^
/usr/include/glib-2.0/gobject/gtype.h:2238:44: note: in definition of macro ‘_G_TYPE_CIT’
   GTypeInstance *__inst = (GTypeInstance*) ip; GType __t = gt; gboolean __r; \
                                            ^~
/usr/include/gtk-2.0/gtk/gtkwidget.h:139:35: note: in expansion of macro ‘G_TYPE_CHECK_INSTANCE_TYPE’
 #define GTK_IS_WIDGET(widget)    (G_TYPE_CHECK_INSTANCE_TYPE ((widget), GTK_TYPE_WIDGET))
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~
main.c:6:3: note: in expansion of macro ‘GTK_IS_WIDGET’
   GTK_IS_WIDGET(new_widget());
   ^~~~~~~~~~~~~
/usr/include/glib-2.0/gobject/gtype.h:2238:27: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
   GTypeInstance *__inst = (GTypeInstance*) ip; GType __t = gt; gboolean __r; \
                           ^
/usr/include/glib-2.0/gobject/gtype.h:495:66: note: in expansion of macro ‘_G_TYPE_CIT’
 #define G_TYPE_CHECK_INSTANCE_TYPE(instance, g_type)            (_G_TYPE_CIT ((instance), (g_type)))
                                                                  ^~~~~~~~~~~
/usr/include/gtk-2.0/gtk/gtkwidget.h:139:35: note: in expansion of macro ‘G_TYPE_CHECK_INSTANCE_TYPE’
 #define GTK_IS_WIDGET(widget)    (G_TYPE_CHECK_INSTANCE_TYPE ((widget), GTK_TYPE_WIDGET))
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~
main.c:6:3: note: in expansion of macro ‘GTK_IS_WIDGET’
   GTK_IS_WIDGET(new_widget());

If I run the the file (./main.o) I receive the "OK" message that confirms that GTK_IS_WIDGET inside the function worked properly, but then a segmentation fault when GTK_IS_WIDGET is called in main.

If I declare the function inside main everything works fine, but this isn't a real solution.

Thanks in advance

Upvotes: 0

Views: 228

Answers (1)

Jussi Kukkonen
Jussi Kukkonen

Reputation: 14577

I get many warnings, (Which I put down there) but no error.

This is a dangerous mindset with C where any warning can absolutely be fatal. I recommend using -Wall with gcc when developing (and only relaxing that for specific cases).

In this case the compiler warns you like this:

main.c:6:17: warning: implicit declaration of function ‘new_widget’

In C this means a default signature of int new_widget() is then used when calling your function. Your actual function signature does not match this of course so bad things start to happen.

You can fix this by adding a header file that contains the correct function signature and by #including that header in your main.c.

Upvotes: 1

Related Questions