GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Jul 23, 2014 9:42 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Segmentation Fault in gtk_editable_get_chars
PostPosted: Tue May 29, 2007 5:29 pm 
Offline

Joined: Tue May 29, 2007 5:14 pm
Posts: 3
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;
/* ... */
};


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 29, 2007 7:42 pm 
Offline
Familiar Face

Joined: Tue Feb 13, 2007 3:45 pm
Posts: 30
Location: Madrid, Spain
Paste here the code where you allocate 'a_entry', please :).

_________________
David Vilches


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 30, 2007 2:16 pm 
Offline

Joined: Tue May 29, 2007 5:14 pm
Posts: 3
======
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;
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 30, 2007 11:27 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
You must initialize the structure before you use it with g_slice_new() or g_malloc().

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 02, 2007 2:22 pm 
Offline

Joined: Tue May 29, 2007 5:14 pm
Posts: 3
Well, could you say it a bit more clearly.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 02, 2007 3:03 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
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:
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.

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group