Connecting Tech Pros Worldwide Forums | Help | Site Map

GTK+, segmentation fault in gtK-editable_get_chars

zombek@gmail.com
Guest
 
Posts: n/a
#1: May 29 '07
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);
*/ ... */

Francine.Neary@googlemail.com
Guest
 
Posts: n/a
#2: May 29 '07

re: GTK+, segmentation fault in gtK-editable_get_chars


On May 29, 3:22 pm, zom...@gmail.com wrote:
Quote:
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.
Quote:
/* ... */
>
}
>
/* ... other file ...*/
>
g_signal_connect(G_OBJECT(main_window->calc_btn), "clicked",
G_CALLBACK(calculate_cb), main_window);
*/ ... */
mark_bluemel@pobox.com
Guest
 
Posts: n/a
#3: May 29 '07

re: GTK+, segmentation fault in gtK-editable_get_chars


On May 29, 3:22 pm, zom...@gmail.com wrote:
Quote:
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.
Quote:
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.

Closed Thread