 |
GTK+ Forums Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
|
|
|
| Author |
Message |
|
|
szymzet
Joined: 29 May 2007 Posts: 3
|
Posted: Tue May 29, 2007 5:29 pm Post subject: Segmentation Fault in gtk_editable_get_chars |
|
|
I put comments in the code, any help is appreciated.
These are the parts I thought are the only necessary ones, if more needed just say so, I'll post them.
===========
signal connection
===========
g_signal_connect(G_OBJECT(main_window->calc_btn), "clicked",
G_CALLBACK(calculate_cb), main_window);
========
callback
==========
void calculate_cb(GtkWidget *widget, GdkEvent *event, MainWindow *main_window)
{
/*...*/
gchar *s;
/* I get Segmentation Fault here,
the window shows up, but when I press the button it terminates with this error */
s = gtk_editable_get_chars(GTK_EDITABLE(main_window->a_entry), 0, -1);
/*...*/
}
===========
MainWindow struct
============
typedef struct _MainWindow MainWindow;
struct _MainWindow
{
/* .. */
GtkWidget *a_entry;
/* ... */
}; |
|
| Back to top |
|
 |
deusvede Familiar Face
Joined: 13 Feb 2007 Posts: 29 Location: Madrid, Spain
|
Posted: Tue May 29, 2007 7:42 pm Post subject: |
|
|
| Paste here the code where you allocate 'a_entry', please :). |
|
| Back to top |
|
 |
szymzet
Joined: 29 May 2007 Posts: 3
|
Posted: Wed May 30, 2007 2:16 pm Post subject: |
|
|
======
main
======
int main(int argc, char *argv[])
{
MainWindow *main_window;
gtk_init(&argc, &argv);
main_window = main_window_create();
gtk_main();
return 0;
}
========
main_window_create
=========
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);
g_signal_connect(G_OBJECT(main_window->window), "destroy",
G_CALLBACK(destroy_cb), NULL);
/* ... */
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();
/* ... */
gtk_entry_set_max_length(GTK_ENTRY(main_window->a_entry), ENTRY_MAX_LEN);
/* ... showing widgets ...*/
return main_window;
} |
|
| Back to top |
|
 |
openldev Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 387 Location: Fairfax, Virginia
|
Posted: Wed May 30, 2007 11:27 pm Post subject: |
|
|
| You must initialize the structure before you use it with g_slice_new() or g_malloc(). |
|
| Back to top |
|
 |
szymzet
Joined: 29 May 2007 Posts: 3
|
Posted: Sat Jun 02, 2007 2:22 pm Post subject: |
|
|
| Well, could you say it a bit more clearly. |
|
| Back to top |
|
 |
openldev Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 387 Location: Fairfax, Virginia
|
Posted: Sat Jun 02, 2007 3:03 pm Post subject: |
|
|
MainWindow is a structure. When you call main_window_create(), you have a pointer called main_window. However, it points to nothing until you initialize it! So, when you try to reference main_window->window, it is trying to reference unallocated memory. Therefore, before you can use the structure, you must call:
| Code: (Plaintext) | 1
| MainWindow *main_window = g_slice_new (MainWindow); |
This will allocate memory for a new MainWindow structure and point main_window to it. If you get a seg fault, it usually means that you are trying to access memory that doesn't exist (unallocated), or you are trying to access memory that was already freed. |
|
| Back to top |
|
 |
|
Powered by phpBB © 2001, 2005 phpBB Group CodeBB 1.0 Beta 2
|