简体   繁体   中英

GTK: g_signal_connect() send wrong address

I have this piece of code:

int gui_showScore(struct Game *game)
{
    printf("%p\n", game);
    return NO_ERROR;
}

int gui_createButtonShowScore(GtkWidget *fixed, GtkWidget **showScore,
                          struct Game *game)
{
    if (fixed == NULL)
        return POINTER_NULL;

    printf("%p\n", game);
    *showScore = gtk_button_new_with_label("Show score");
    gtk_fixed_put(GTK_FIXED(fixed), *showScore, 620, 50);
    gtk_widget_show(*showScore);
    g_signal_connect(G_OBJECT(*showScore), "clicked",
                     G_CALLBACK(gui_showScore), game);

    return NO_ERROR;
}

Why when is pressed the button, is called gui_showScore() with other pointer?

eg: In gui_createButtonShowScore() the pointer value game is 0x1249590 and when is pressed the button is called gui_showScore() and the pointer value game is 0x12202b0. Why?

How can resolve this problem?

Read the documentation for the "clicked" signal :

void user_function(GtkButton *button, gpointer   user_data)

The first argument is always always the object that received the signal, the last argument is the user pointer where the data supplied when you connected the handler is passed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM