By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,907 Members | 1,364 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,907 IT Pros & Developers. It's quick & easy.

GTK+, segmentation fault in gtK-editable_get_chars

P: n/a
Hello.
I wrote a simple GTK+ program. On clicking a button I use calculate_cb
callback function, and send MainWindow structure (it contains widgets)
as data. When inside the callback function I try to assign
gtk_editable_get_chars(GTK_EDITABLE(main_window->a_entry), 0, -1) to s
I get segmentation fault.
Could anyone help me in finding the bug.

Parts of the code:

void calculate_cb(GtkWidget *widget, GdkEvent *event, MainWindow
*main_window)
{
/* ... */
gchar *s;

s = (gchar *)g_malloc((ENTRY_MAX_LEN + 1) * sizeof(gchar));

s = gtk_editable_get_chars(GTK_EDITABLE(main_window->a_entry),
0, -1);
/* ... */
}

/* ... other file ...*/

g_signal_connect(G_OBJECT(main_window->calc_btn), "clicked",
G_CALLBACK(calculate_cb), main_window);
*/ ... */

May 29 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On May 29, 3:22 pm, zom...@gmail.com wrote:
Hello.
I wrote a simple GTK+ program. On clicking a button I use calculate_cb
callback function, and send MainWindow structure (it contains widgets)
as data. When inside the callback function I try to assign
gtk_editable_get_chars(GTK_EDITABLE(main_window->a_entry), 0, -1) to s
I get segmentation fault.
Could anyone help me in finding the bug.

Parts of the code:

void calculate_cb(GtkWidget *widget, GdkEvent *event, MainWindow
*main_window)
{
/* ... */
gchar *s;

s = (gchar *)g_malloc((ENTRY_MAX_LEN + 1) * sizeof(gchar));

s = gtk_editable_get_chars(GTK_EDITABLE(main_window->a_entry),
0, -1);
Assuming g_malloc is a wrapper from malloc (and if so, why are you
casting the return value?), you've just got yourself a memory leak. If
gtk_editable_get_chars returns a char *, it most likely allocates the
memory for it itself, and you need to free() it (check the
documentation).

As for the segfault, it's completely impossible to tell why this line
produces it without knowing
(1) whether main_window is a valid pointer to a struct containing an
a_entry field, and
(2) what gtk_editable_get_chars and GTK_EDITABLE do.
Unfortunately for you, (2) is a question about a non-standard-C API,
so it's off-topic here.

My impression is that you have very little idea about how memory
allocation works in C - you'd be much better off learning C thoroughly
(and studying the excellent on-topic posts here) rather than plunging
in with some big baroque toolkit.
/* ... */

}

/* ... other file ...*/

g_signal_connect(G_OBJECT(main_window->calc_btn), "clicked",
G_CALLBACK(calculate_cb), main_window);
*/ ... */
May 29 '07 #2

P: n/a
On May 29, 3:22 pm, zom...@gmail.com wrote:
Hello.
I wrote a simple GTK+ program. On clicking a button I use calculate_cb
callback function, and send MainWindow structure (it contains widgets)
as data. When inside the callback function I try to assign
gtk_editable_get_chars(GTK_EDITABLE(main_window->a_entry), 0, -1) to s
I get segmentation fault.
Could anyone help me in finding the bug.

Parts of the code:

void calculate_cb(GtkWidget *widget, GdkEvent *event, MainWindow
*main_window)
{
/* ... */
gchar *s;

s = (gchar *)g_malloc((ENTRY_MAX_LEN + 1) * sizeof(gchar));
So you've allocated some space and stored the address in "s". As
Francine suggests, it's probably not necessary (or helpful) to cast
the return value from g_malloc.
s = gtk_editable_get_chars(GTK_EDITABLE(main_window->a_entry),
0, -1);
Now you've replaced the value in "s" with some space returned by
gtk_editable_get_chars. Why?

Other than that, I think you'd be better off asking in a forum about
GTK+, rather than here. It shouldn't take long to find one.

May 29 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.