Unable to destroy parent on window close GTK4

I am learning on how to use GTK4. The code below sets a button to a window and when clicked, it pops up a emoji chooser. When an emoji is clicked, the button label is changed to the emoji chosen.

Everything seems to be working, but when I close the window I get these errors:

(gtk_example:235120): Gtk-WARNING **: 11:35:14.635: Finalizing GtkButton 0x563286217490, but it still has children left:

(gtk_example:235120): Gtk-WARNING **: 11:35:14.636:    - GtkEmojiChooser 0x563286614220

Code

#include <gtk/gtk.h>

static void on_emoji_chooser_activated(GtkEmojiChooser *emoji_chooser, gchar *text, gpointer user_data) {
    GtkWidget *button = GTK_WIDGET(user_data);
    if (!GTK_IS_BUTTON(button)) {
        g_warning("Widget is not a GtkButton!");
        return;
    }

    if (!g_utf8_validate(text, -1, NULL)) {
        g_warning("The emoji string is not valid UTF-8.");
        return;
    }

    gtk_button_set_label(GTK_BUTTON(button), text);
}

static void on_button_clicked(GtkButton *button, gpointer user_data) {
    GtkWidget *emoji_chooser = GTK_WIDGET(user_data);
    gtk_popover_popup(GTK_POPOVER(emoji_chooser));
}

static void on_window_destroy(GtkWindow *window, gpointer user_data) {
    GtkWidget *emoji_chooser = GTK_WIDGET(user_data);
    // Unparent the emoji chooser
    g_object_unref(emoji_chooser);
}

static void on_activate(GtkApplication *app) {
    // Create a new window
    GtkWidget *window = gtk_application_window_new(app);
    // Create a new button
    GtkWidget *button = gtk_button_new();
    // Set the button label
    gtk_button_set_label(GTK_BUTTON(button), "Click me!");
    // Create a new emoji chooser and set the button as its parent
    GtkWidget *emoji_chooser = gtk_emoji_chooser_new();
    gtk_widget_set_parent(emoji_chooser, button);

    // When the button is clicked, show the emoji chooser
    g_signal_connect(button, "clicked", G_CALLBACK(on_button_clicked), emoji_chooser);
    // When an emoji is selected, set the button label to the emoji
    g_signal_connect(emoji_chooser, "emoji-picked", G_CALLBACK(on_emoji_chooser_activated), button);
    // When the window is destroyed, destroy the emoji chooser
    g_signal_connect(window, "destroy", G_CALLBACK(on_window_destroy), emoji_chooser);

    gtk_window_set_child(GTK_WINDOW(window), button);
    gtk_window_present(GTK_WINDOW(window));
}

int main(int argc, char *argv[]) {
    // Create a new application
    GtkApplication *app = gtk_application_new("com.gollahalli.GtkExample",
                                              G_APPLICATION_FLAGS_NONE);
    g_signal_connect (app, "activate", G_CALLBACK(on_activate), NULL);
    return g_application_run(G_APPLICATION (app), argc, argv);
}

I feel the issue is happening when I do gtk_widget_set_parent(emoji_chooser, button);.

Another approach I tried was to unparent the EmojiChooser, by doing

static void on_window_destroy(GtkWindow *window, gpointer user_data) {
    GtkWidget *emoji_chooser = GTK_WIDGET(user_data);
    gtk_widget_unparent(emoji_chooser);
    g_object_unref(emoji_chooser);
}

This gave me assertion error of invalid unclassed pointer in cast to 'GtkAccessible'.

I have see this error whenever I use a popup.

I am not sure what I am doing wrong, any help is appreciated.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.