Simple gtk+ app, seg fault

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-
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.
 
R

Richard Heathfield

(e-mail address removed) said:

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
<
<huge snip>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top