Thread: segmentation fault?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    3

    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;
    }

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    3
    ok I run ddd and I se data is a NULL-pointer. SO im on the track.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    3
    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?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM