GTK+, segmentation fault in gtK-editable_get_chars

Discussion in 'C Programming' started by zombek@gmail.com, May 29, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. Guest

    On May 29, 3:22 pm, 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, 2007
    #2
    1. Advertising

  3. Guest

    On May 29, 3:22 pm, 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, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Hunsley
    Replies:
    17
    Views:
    869
  2. Pud
    Replies:
    0
    Views:
    578
  3. Replies:
    0
    Views:
    530
  4. Ivan Vecerina
    Replies:
    0
    Views:
    486
    Ivan Vecerina
    Jun 29, 2003
  5. Vasileios Zografos

    Re: segmentation fault exception handling

    Vasileios Zografos, Jun 30, 2003, in forum: C++
    Replies:
    5
    Views:
    15,621
    Pete Becker
    Jul 1, 2003
Loading...

Share This Page