Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8759773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:53:12+00:00 2026-06-13T14:53:12+00:00

I have a callback function that needs to access several GTK widgets. To enable

  • 0

I have a callback function that needs to access several GTK widgets. To enable this I am trying to send pointers to the relevant GTK widgets packaged in a structure as an argument to the callback function (instead of using global variables to enable access). To structure the data in a nice way I use a structure of pointers to structures of pointers to GTK widgets.

My problem is that I get segmentation fault when trying to read from or write to the GTK widgets in the callback function. How can I avoid this? As I just started learning C and GTK my approach may be inappropriate altogether, so I would also appreciate pointers on the more general of problem of accessing several GTK widgets from callback functions.

In trying to fix the problem I have managed to boil down the code that presents the problem to the following. It segfaults when changing the value of the spinbutton. It also segfault if one lets the last line in widget_change_value execute instead.

/* Compile with gcc foo.c `pkg-config --cflags --libs gtk+-2.0` */
#include <gtk/gtk.h> /* Get e.g. via sudo apt-get install libgtk2.0-dev */

typedef struct {
    GtkWidget *widget;
} s_input;

typedef struct {
    s_input *input;
} s_data;

void widget_change_value(s_data *data) {
    /* Segfault when trying to read from data  */
    int number = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(data->input->widget));

    /* /\* Segfault when trying to write to data *\/ */
    /* gtk_spin_button_set_value(GTK_SPIN_BUTTON(data->input->widget), (gdouble) 2.0); */
}

int main(int argc, char *argv[])
{
    GtkWidget *window;
    s_input input;
    s_data data = { &input };

    gtk_init(&argc, &argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    input.widget = gtk_spin_button_new_with_range(0, 3, 1);
    gtk_container_add(GTK_CONTAINER(window), input.widget);
    g_signal_connect(GTK_OBJECT(input.widget), "value_changed", GTK_SIGNAL_FUNC(widget_change_value), (gpointer *) &data);

    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:53:13+00:00Added an answer on June 13, 2026 at 2:53 pm

    Your crash is because your callback function’s prototype is all wrong.

    You have:

    void widget_change_value(s_data *data);
    

    but looking at the docs, the value-changed signal handler callback is declared as:

    void user_function(GtkSpinButton *spinbutton, gpointer user_data);
    

    You need to add the missing widget pointer argument.

    Also:

    1. Your callback should be static, since it’s very local to the C file it’s in.
    2. You don’t need to cast things to gpointer.
    3. The cast to (gpointer *) is doubly wrong, since you’re passing a pointer (&data) which you want to convert to gpointer, not to “pointer to gpointer“. The asterisk is wrong. But, as I said above, the entire cast is pointless and should be removed.
    4. Consider porting to a more recent GTK+ version, you’re using some obsolete things (GTK_OBJECT() instead of G_OBJECT(), GTK_SIGNAL_FUNC() instead of G_CALLBACK(), for instance).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have am inside a callback that allows me to access the pointers passed
I have a C library that needs a callback function to be registered to
I have a callback function that checks my login details are correct - If
I have a function that I use as a callback for a custom event:
I have a member function in a class that has a callback, but the
I want to have one callback function after actions are done, I'm trying something
I have this static callback function in MyClass, and I try to call another
I have a jQuery plugin I am trying to add a callback function for.
I have a library-provided function that runs a callback after some processing. Inside that
I have a C library function that expects a function pointer for callback, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.