Do I need to free popup menus in GTK?
c, gtk
Solution
`GtkMenu`, as seen in your code, is a subclass of `GtkWidget`, which in turn is a subclass of `GInitiallyUnowned`. So it has all that floating-ref magic around.
When you popup a menu, it works just like `GtkWindow`, so it automatically ref-sinks the floating reference, and eventually, when the menu is dismissed, it is unreferenced and freed.
Short answer: Yes, it is automatic, so your code is correct.
You can check if I'm right with this code:
g_object_ref_sink(menu); //ref = 1
g_menu_popup(...);
g_print("I am %s\n", menu->ref_count==1? "right" : "wrong");
g_object_unref(menu);
Note: do not use `ref_count` for anything but debugging! It should be considered an implementation detail of `GObject` and never be accessed directly.
Problem
This is the first time I'm using GTK. I have the following code and I'm wondering if it is leaking memory. It's inside a function that is called every time a right click happens. ``` GtkWidget *menu = gtk_menu_new(); //while loop adding a bunch of menu items gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item); gtk_widget_show_all(menu); gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, 3, event->button.time); ``` Is the cleaning handled automatically by GTK?