Z
zombek
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-
/* 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.
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-
show_all_widgets(main_window);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.