Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Simple gtk+ app, seg fault

Reply
Thread Tools

Simple gtk+ app, seg fault

 
 
zombek@gmail.com
Guest
Posts: n/a
 
      05-28-2007
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.

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      05-28-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
seg-fault on vector-auto-reserve Matthias Kaeppler C++ 2 02-27-2005 10:01 PM
seg fault on different linux boxes with C++. goktan C++ 4 08-02-2004 03:25 PM
fstream::open & seg fault Manuel Maria Diaz Gomez C++ 3 07-20-2004 06:18 PM
seg fault because stdexcept.cc not found? Corrine C++ 1 11-27-2003 06:15 AM
Suprising seg fault!!! Vinod C++ 3 09-27-2003 02:43 PM



Advertisments