GTK+, segmentation fault in gtK-editable_get_chars

Z

zombek

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);
*/ ... */
 
F

Francine.Neary

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.
 
M

mark_bluemel

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top