-
segmentation fault?
Hi,
Im new to this forum and I like what i seen so far. Could anyone tell me whats wrong with this program below. I get a segmentation fault when pressing the button.
Code:
#include <gtk/gtk.h>
typedef enum { APPLE, BANANA } Fruit_Type;
typedef struct Fruit {Fruit_Type ft;} Fruit;
void PealAFruit(Fruit_Type fruit)
{
g_message ("PealAFruit called");
}
static gboolean button_clicked( GtkWidget *widget,
GdkEventButton *event,
gpointer data)
{
Fruit *afruit = (Fruit*)data;
PealAFruit(afruit->ft); /*here a segmentation fault */
return TRUE;
}
int main( int argc, char *argv[] )
{
g_message ("start");
GtkWidget *window;
GtkWidget *button;
Fruit *afruit;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
button = gtk_button_new_with_label ("Hello World");
if ( (afruit = (Fruit*)g_malloc(sizeof(Fruit))) != NULL)
{
afruit->ft = APPLE;
g_signal_connect (G_OBJECT (button), "clicked",
G_CALLBACK (button_clicked), afruit);
}
gtk_container_add (GTK_CONTAINER (window), button);
gtk_widget_show (button);
gtk_widget_show (window);
gtk_main ();
return 0;
}
-
ok I run ddd and I se data is a NULL-pointer. SO im on the track.
-
http://developer.gnome.org/doc/API/2...t-Signals.html
A quick browse through the API suggests that g_signal_new() might be a pre-requisite call.
g_signal_connect also returns a value - is it valid?
-
The return value i null so I guess I alocat the memory badly. I just want to send a pointer of the struct to the function. Should not be this hard. Im trying to check it in GDB and DDD. But since I am new to c and these programs its hard.
How do I print a pointervalue, the addressto check that I send a value?
-
Return value from what?
Read the manual, if g_signal_connect() is returning NULL, then I'm guessing it means the whole thing failed because you need to call g_signal_new() first, and perhaps some other stuff as well.
It's like calling fgets() without calling fopen(). If you don't do all the required stuff first, then the call itself makes no sense.