Simple gtk+ app, seg fault

Discussion in 'C Programming' started by zombek@gmail.com, May 28, 2007.

  1. Guest

    I'm trying to learn gtk+ and I have the following app (look below), it
    compiles correctly, but when I try to run it I get segmentation fault
    error, I'd appreciate any help in finding the error.

    _____________________________
    main.c
    _____________________________
    #include <stdio.h>

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

    #include "main_window.h"

    int main(int argc, char *argv[])
    {
    MainWindow *main_window;

    gtk_init(&argc, &argv);

    main_window = main_window_create();

    gtk_main();

    return 0;
    }

    __________________________
    main_window.h
    __________________________
    #ifndef MAIN_WINDOW_H
    #define MAIN_WINDOW_H

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

    #define WINDOW_TITLE "quadratic equation"

    typedef struct _MainWindow MainWindow;

    struct _MainWindow
    {
    /* window */
    GtkWidget *window;

    /* widgets */
    GtkWidget *a_entry;

    ETC...................

    GtkWidget *calc_btn;

    GtkWidget *table;
    };

    MainWindow *main_window_create(void);
    #endif // MAIN_WINDOW_H

    ____________________________
    main_window.c
    _____________________________
    #include <glib.h>
    ETC............
    #include <gtk/gtklabel.h>

    #include "main_window.h"
    #include "calculate.h"

    static gboolean main_window_close_cb(GtkWidget *widget, GdkEvent
    *event,
    gpointer data);

    static void show_all_widgets(MainWindow *main_window);

    MainWindow *main_window_create(void)
    {
    MainWindow *main_window;

    main_window->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(main_window->window),
    WINDOW_TITLE);

    g_signal_connect(G_OBJECT(main_window->window),
    "delete_event",
    G_CALLBACK(main_window_close_cb), NULL);

    main_window->table = gtk_table_new(5, 2, TRUE);
    gtk_container_add(GTK_CONTAINER(main_window->window),
    main_window->table);

    main_window->a_label = gtk_label_new("a = ");
    main_window->b_label = gtk_label_new("b = ");
    main_window->c_label = gtk_label_new("c = ");

    main_window->calc_btn =
    gtk_button_new_with_label("calculate");
    g_signal_connect(G_OBJECT(main_window->calc_btn), "clicked",
    G_CALLBACK(calculate_cb), main_window);

    main_window->a_entry = gtk_entry_new();
    main_window->b_entry = gtk_entry_new();
    main_window->c_entry = gtk_entry_new();
    main_window->x_entry = gtk_entry_new();

    gtk_editable_set_editable(GTK_EDITABLE(main_window->x_entry),
    FALSE);

    /* it doesn't show the main window widget (main_window-
    >window) */

    show_all_widgets(main_window);

    /* put the widgets in the table */
    gtk_table_attach_defaults(GTK_TABLE(main_window->table),
    main_window->a_label, 0, 1, 0, 1);
    gtk_table_attach_defaults(GTK_TABLE(main_window->table),
    main_window->a_entry, 1, 2, 0, 1);
    gtk_table_attach_defaults(GTK_TABLE(main_window->table),
    main_window->b_label, 0, 1, 1, 2);
    gtk_table_attach_defaults(GTK_TABLE(main_window->table),
    main_window->b_entry, 1, 2, 1, 2);
    gtk_table_attach_defaults(GTK_TABLE(main_window->table),
    main_window->c_label, 0, 1, 2, 3);
    gtk_table_attach_defaults(GTK_TABLE(main_window->table),
    main_window->c_entry, 1, 2, 2, 3);
    gtk_table_attach_defaults(GTK_TABLE(main_window->table),
    main_window->x_entry, 0, 2, 3, 4);
    gtk_table_attach_defaults(GTK_TABLE(main_window->table),
    main_window->calc_btn, 1, 2, 4, 5);

    gtk_widget_show(main_window->window);

    return main_window;
    }

    static gboolean main_window_close_cb(GtkWidget *widget, GdkEvent
    *event,
    gpointer data)
    {
    return TRUE;
    }

    static void show_all_widgets(MainWindow *main_window)
    {
    /*
    * it doesn't show the window widget
    * */
    gtk_widget_show(main_window->a_entry);
    gtk_widget_show(main_window->b_entry);
    gtk_widget_show(main_window->c_entry);
    gtk_widget_show(main_window->x_entry);
    gtk_widget_show(main_window->a_label);
    gtk_widget_show(main_window->b_label);
    gtk_widget_show(main_window->c_label);
    gtk_widget_show(main_window->calc_btn);
    gtk_widget_show(main_window->table);
    }

    The calculate file contains only one simple callback function, it's
    not important.
    , May 28, 2007
    #1
    1. Advertising

  2. said:

    <huge snip>

    > MainWindow *main_window;
    >
    > main_window->window =


    Stop.

    main_window is a pointer with an indeterminate value. That is, its job
    is to point at a MainWindow object, but it doesn't do that *yet*,
    because you haven't told it to.

    But *even though* you haven't given it a MainWindow object to point at,
    you've treated it *as if* it were pointing at such an object.

    The behaviour of the program is therefore undefined, and a segfault
    would certainly not be a surprising outcome.


    It seems that your GTK+ question was topical in comp.lang.c by accident
    rather than by design - it did turn out to be a C problem rather than a
    GTK+ problem - but this is not a state of affairs that is likely to
    continue indefinitely. I suggest that you (a) put GTK+ aside until
    you've learned C, and then (b) ask future GTK+ questions in whichever
    group is recommended to you by the good folks in
    <news:comp.os.linux.development.apps>.

    <huge snip>

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, May 28, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vinod

    Suprising seg fault!!!

    Vinod, Sep 27, 2003, in forum: C++
    Replies:
    3
    Views:
    460
    Ron Natalie
    Sep 27, 2003
  2. Corrine
    Replies:
    1
    Views:
    415
    Gianni Mariani
    Nov 27, 2003
  3. Manuel Maria Diaz Gomez

    fstream::open & seg fault

    Manuel Maria Diaz Gomez, Jul 20, 2004, in forum: C++
    Replies:
    3
    Views:
    1,174
    John Harrison
    Jul 20, 2004
  4. goktan
    Replies:
    4
    Views:
    2,014
    Howard
    Aug 2, 2004
  5. Replies:
    2
    Views:
    509
Loading...

Share This Page