473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Simple gtk+ app, seg fault

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 '07 #1
1 1994
zo****@gmail.com 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.
May 28 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Wil Koenen | last post by:
Hi, When importing (parts of) gtk, here's what I get: >>> import gobject Traceback (most recent call last): File "<pyshell#13>", line 1, in ? import gobject ImportError: DLL load failed:...
2
by: Dennis | last post by:
The setup: Windows XP Pro Python 2.3 GTK+ 2.2.1.2 and pyGTK 1.99.17 for Python 2.3 from http://www.pcpm.ucl.ac.be/~gustin/win32_ports/ I've downloaded and installed per the instructions the...
6
by: Batista, Facundo | last post by:
I'm still deciding about using Tkinter or wxPython for a project I have (sigefi, on SF). The decision is not made, I have to finish first a little program that will be done in both GUIs, and...
1
by: kristian.hermansen | last post by:
keherman@ibmlnx20:/tmp$ cat helloworld.py #!/usr/bin/env python import pygtk pygtk.require('2.0')
6
by: johnmmcparland | last post by:
Hi all, I am doing some C / C++ programming in cygwin and I notice when I add something to my path then try to compile, the gcc / g++ compiler cannot find some files, even though they are in the...
6
by: kdikert | last post by:
I have a worker thread, and when the worker is done I want it to show a popup dialog. The sample application below demonstrates this. There are two buttons: a "Show dialog" button which immediately...
8
kaarthikeyapreyan
by: kaarthikeyapreyan | last post by:
The gtk version which tried to install is 2.12.9 The Glib version is 2.16.3 /bin/sh ../libtool --mode=link gcc -g -O2 -Wall -o gtk-query-immodules-2.0 queryimmodules.o libgtk-x11-2.0.la...
1
kaarthikeyapreyan
by: kaarthikeyapreyan | last post by:
This time am getting a new error gcc -DHAVE_CONFIG_H -I. -I. -I.. -DG_LOG_DOMAIN=\"Gtk\" -DGTK_LIBDIR=\"/home/numenta/gtk+-2.9.4/gtkconf/lib\"...
0
by: Joel Hedlund | last post by:
Hi! I'm developing a pygtk application where I need to show images zoomed in so that the user can see individual pixels. gtk.gdk.Pixbuf.scale() seemed ideal for this, but if I set offset_x and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.