From 5c675fdac97d1e9430d3f7fa3589ad372078759e Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sun, 1 Jan 2023 05:20:05 +0200 Subject: [PATCH 43/43] gtk4: Fix menu item leaks See osdn #46295 Signed-off-by: Marko Lindqvist --- client/gui-gtk-4.0/citydlg.c | 26 ++++++--------- client/gui-gtk-4.0/cityrep.c | 12 +++---- client/gui-gtk-4.0/diplodlg.c | 46 +++++++++++++------------- client/gui-gtk-4.0/gui_stuff.h | 16 ++++++++- client/gui-gtk-4.0/menu.c | 59 +++++++++++++++------------------- client/gui-gtk-4.0/optiondlg.c | 15 ++++----- client/gui-gtk-4.0/pages.c | 39 +++++++++------------- client/gui-gtk-4.0/plrdlg.c | 52 ++++++++++++------------------ client/gui-gtk-4.0/wldlg.c | 10 +++--- 9 files changed, 126 insertions(+), 149 deletions(-) diff --git a/client/gui-gtk-4.0/citydlg.c b/client/gui-gtk-4.0/citydlg.c index acacc4154a..4957b6edb3 100644 --- a/client/gui-gtk-4.0/citydlg.c +++ b/client/gui-gtk-4.0/citydlg.c @@ -2474,7 +2474,6 @@ static bool create_unit_menu(struct city_dialog *pdialog, struct unit *punit, GtkWidget *wdg, bool supported) { GMenu *menu; - GMenuItem *item; GActionGroup *group; GSimpleAction *act; @@ -2495,8 +2494,7 @@ static bool create_unit_menu(struct city_dialog *pdialog, struct unit *punit, g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); g_signal_connect(act, "activate", G_CALLBACK(unit_center_callback), GINT_TO_POINTER(punit->id)); - item = g_menu_item_new(_("Cen_ter"), "win.center"); - g_menu_append_item(menu, item); + menu_item_append_unref(menu, g_menu_item_new(_("Cen_ter"), "win.center")); } act = g_simple_action_new("activate", NULL); @@ -2504,8 +2502,7 @@ static bool create_unit_menu(struct city_dialog *pdialog, struct unit *punit, g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); g_signal_connect(act, "activate", G_CALLBACK(unit_activate_callback), GINT_TO_POINTER(punit->id)); - item = g_menu_item_new(_("_Activate unit"), "win.activate"); - g_menu_append_item(menu, item); + menu_item_append_unref(menu, g_menu_item_new(_("_Activate unit"), "win.activate")); act = g_simple_action_new("activate_close", NULL); g_object_set_data(G_OBJECT(act), "dlg", pdialog); @@ -2519,8 +2516,8 @@ static bool create_unit_menu(struct city_dialog *pdialog, struct unit *punit, GINT_TO_POINTER(punit->id)); } - item = g_menu_item_new(_("Activate unit, _close dialog"), "win.activate_close"); - g_menu_append_item(menu, item); + menu_item_append_unref(menu, g_menu_item_new(_("Activate unit, _close dialog"), + "win.activate_close")); if (!supported) { act = g_simple_action_new("load", NULL); @@ -2528,43 +2525,39 @@ static bool create_unit_menu(struct city_dialog *pdialog, struct unit *punit, g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); g_signal_connect(act, "activate", G_CALLBACK(unit_load_callback), GINT_TO_POINTER(punit->id)); - item = g_menu_item_new(_("_Load unit"), "win.load"); g_simple_action_set_enabled(G_SIMPLE_ACTION(act), unit_can_load(punit)); - g_menu_append_item(menu, item); + menu_item_append_unref(menu, g_menu_item_new(_("_Load unit"), "win.load")); act = g_simple_action_new("unload", NULL); g_object_set_data(G_OBJECT(act), "dlg", pdialog); g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); g_signal_connect(act, "activate", G_CALLBACK(unit_unload_callback), GINT_TO_POINTER(punit->id)); - item = g_menu_item_new(_("_Unload unit"), "win.unload"); g_simple_action_set_enabled(G_SIMPLE_ACTION(act), can_unit_unload(punit, unit_transport_get(punit)) && can_unit_exist_at_tile(&(wld.map), punit, unit_tile(punit))); - g_menu_append_item(menu, item); + menu_item_append_unref(menu, g_menu_item_new(_("_Unload unit"), "win.unload")); act = g_simple_action_new("sentry", NULL); g_object_set_data(G_OBJECT(act), "dlg", pdialog); g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); g_signal_connect(act, "activate", G_CALLBACK(unit_sentry_callback), GINT_TO_POINTER(punit->id)); - item = g_menu_item_new(_("_Sentry unit"), "win.sentry"); g_simple_action_set_enabled(G_SIMPLE_ACTION(act), punit->activity != ACTIVITY_SENTRY && can_unit_do_activity(punit, ACTIVITY_SENTRY)); - g_menu_append_item(menu, item); + menu_item_append_unref(menu, g_menu_item_new(_("_Sentry unit"), "win.sentry")); act = g_simple_action_new("fortify", NULL); g_object_set_data(G_OBJECT(act), "dlg", pdialog); g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); g_signal_connect(act, "activate", G_CALLBACK(unit_fortify_callback), GINT_TO_POINTER(punit->id)); - item = g_menu_item_new(_("_Fortify unit"), "win.fortify"); g_simple_action_set_enabled(G_SIMPLE_ACTION(act), punit->activity != ACTIVITY_FORTIFYING && can_unit_do_activity(punit, ACTIVITY_FORTIFYING)); - g_menu_append_item(menu, item); + menu_item_append_unref(menu, g_menu_item_new(_("_Fortify unit"), "win.fortify")); } act = g_simple_action_new("disband", NULL); @@ -2572,10 +2565,9 @@ static bool create_unit_menu(struct city_dialog *pdialog, struct unit *punit, g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); g_signal_connect(act, "activate", G_CALLBACK(unit_disband_callback), GINT_TO_POINTER(punit->id)); - item = g_menu_item_new(_("_Disband unit"), "win.disband"); g_simple_action_set_enabled(G_SIMPLE_ACTION(act), unit_can_do_action(punit, ACTION_DISBAND_UNIT)); - g_menu_append_item(menu, item); + menu_item_append_unref(menu, g_menu_item_new(_("_Disband unit"), "win.disband")); pdialog->popover = gtk_popover_menu_new_from_model(G_MENU_MODEL(menu)); g_object_ref(pdialog->popover); diff --git a/client/gui-gtk-4.0/cityrep.c b/client/gui-gtk-4.0/cityrep.c index 294f6c8aa0..bc4045ecae 100644 --- a/client/gui-gtk-4.0/cityrep.c +++ b/client/gui-gtk-4.0/cityrep.c @@ -1017,7 +1017,8 @@ static void city_report_update_views(void) } /************************************************************************//** - Create up-to-date menu item for the display menu + Create up-to-date menu item for the display menu. + Caller need to g_object_unref() returned item. ****************************************************************************/ static GMenuItem *create_display_menu_item(int pos) { @@ -1045,7 +1046,7 @@ static void toggle_view(GSimpleAction *act, GVariant *value, gpointer data) city_report_update_views(); g_menu_remove(display_menu, idx); - g_menu_insert_item(display_menu, idx, create_display_menu_item(idx)); + menu_item_insert_unref(display_menu, idx, create_display_menu_item(idx)); } /************************************************************************//** @@ -1068,7 +1069,7 @@ static GMenu *create_display_menu(GActionGroup *group) g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); g_signal_connect(act, "change-state", G_CALLBACK(toggle_view), (gpointer)spec); - g_menu_insert_item(display_menu, i, create_display_menu_item(i)); + menu_item_insert_unref(display_menu, i, create_display_menu_item(i)); } g_variant_type_free(bvart); @@ -1087,7 +1088,6 @@ static GtkWidget *create_city_report_menu(void) GActionGroup *group; GMenu *submenu; GSimpleAction *act; - GMenuItem *item; vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); sep = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL); @@ -1104,8 +1104,8 @@ static GtkWidget *create_city_report_menu(void) g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); g_signal_connect(act, "activate", G_CALLBACK(city_clear_worklist_callback), NULL); - item = g_menu_item_new(_("Clear _Worklist"), "win.clear_worklist"); - g_menu_append_item(submenu, item); + menu_item_append_unref(submenu, g_menu_item_new(_("Clear _Worklist"), + "win.clear_worklist")); g_menu_append_submenu(menu, _("_Production"), G_MENU_MODEL(submenu)); diff --git a/client/gui-gtk-4.0/diplodlg.c b/client/gui-gtk-4.0/diplodlg.c index ed57f5bad3..9fd3c73899 100644 --- a/client/gui-gtk-4.0/diplodlg.c +++ b/client/gui-gtk-4.0/diplodlg.c @@ -263,7 +263,6 @@ static GMenu *create_clause_menu(GActionGroup *group, struct player *partner, bool them) { GMenu *topmenu, *submenu; - GMenuItem *item; GSimpleAction *act; bool any_map = FALSE; char act_plr_part[20]; @@ -294,8 +293,7 @@ static GMenu *create_clause_menu(GActionGroup *group, pdialog); fc_snprintf(act_name, sizeof(act_name), "win.worldmap%s", act_plr_part); - item = g_menu_item_new(_("World-map"), act_name); - g_menu_append_item(submenu, item); + menu_item_append_unref(submenu, g_menu_item_new(_("World-map"), act_name)); any_map = TRUE; } @@ -313,8 +311,7 @@ static GMenu *create_clause_menu(GActionGroup *group, pdialog); fc_snprintf(act_name, sizeof(act_name), "win.seamap%s", act_plr_part); - item = g_menu_item_new(_("Sea-map"), act_name); - g_menu_append_item(submenu, item); + menu_item_append_unref(submenu, g_menu_item_new(_("Sea-map"), act_name)); any_map = TRUE; } @@ -365,8 +362,7 @@ static GMenu *create_clause_menu(GActionGroup *group, GINT_TO_POINTER(A_LAST)); fc_snprintf(act_name, sizeof(act_name), "win.advance%sall", act_plr_part); - item = g_menu_item_new(_("All advances"), act_name); - g_menu_append_item(submenu, item); + menu_item_append_unref(submenu, g_menu_item_new(_("All advances"), act_name)); for (list_item = sorting_list, i = 0; NULL != list_item; list_item = g_list_next(list_item), i++) { @@ -387,8 +383,9 @@ static GMenu *create_clause_menu(GActionGroup *group, fc_snprintf(act_name, sizeof(act_name), "win.advance%s%d", act_plr_part, i); - item = g_menu_item_new(advance_name_translation(padvance), act_name); - g_menu_append_item(submenu, item); + menu_item_append_unref(submenu, + g_menu_item_new(advance_name_translation(padvance), + act_name)); } g_list_free(sorting_list); @@ -442,8 +439,9 @@ static GMenu *create_clause_menu(GActionGroup *group, fc_snprintf(act_name, sizeof(act_name), "win.city%s%d", act_plr_part, i); - item = g_menu_item_new(city_name_get(city_list_ptrs[j]), act_name); - g_menu_append_item(submenu, item); + menu_item_append_unref(submenu, + g_menu_item_new(city_name_get(city_list_ptrs[j]), + act_name)); } } @@ -463,10 +461,10 @@ static GMenu *create_clause_menu(GActionGroup *group, pdialog); fc_snprintf(act_name, sizeof(act_name), "win.vision%s", act_plr_part); - item = g_menu_item_new(_("_Give shared vision"), act_name); - g_menu_append_item(topmenu, item); + menu_item_append_unref(topmenu, g_menu_item_new(_("_Give shared vision"), act_name)); - g_simple_action_set_enabled(G_SIMPLE_ACTION(act), !gives_shared_vision(pgiver, pother)); + g_simple_action_set_enabled(G_SIMPLE_ACTION(act), + !gives_shared_vision(pgiver, pother)); } /* Give embassy. */ @@ -479,8 +477,7 @@ static GMenu *create_clause_menu(GActionGroup *group, pdialog); fc_snprintf(act_name, sizeof(act_name), "win.embassy%s", act_plr_part); - item = g_menu_item_new(_("Give _embassy"), act_name); - g_menu_append_item(topmenu, item); + menu_item_append_unref(topmenu, g_menu_item_new(_("Give _embassy"), act_name)); /* Don't take in account the embassy effects. */ g_simple_action_set_enabled(G_SIMPLE_ACTION(act), @@ -497,8 +494,7 @@ static GMenu *create_clause_menu(GActionGroup *group, pdialog); fc_snprintf(act_name, sizeof(act_name), "win.tiles%s", act_plr_part); - item = g_menu_item_new(_("_Share tiles"), act_name); - g_menu_append_item(topmenu, item); + menu_item_append_unref(topmenu, g_menu_item_new(_("_Share tiles"), act_name)); } /* Pacts. */ @@ -518,8 +514,9 @@ static GMenu *create_clause_menu(GActionGroup *group, G_CALLBACK(diplomacy_dialog_ceasefire_callback), pdialog); fc_snprintf(act_name, sizeof(act_name), "win.ceasefire%s", act_plr_part); - item = g_menu_item_new(Q_("?diplomatic_state:Cease-fire"), act_name); - g_menu_append_item(submenu, item); + menu_item_append_unref(submenu, + g_menu_item_new(Q_("?diplomatic_state:Cease-fire"), + act_name)); g_simple_action_set_enabled(G_SIMPLE_ACTION(act), ds != DS_CEASEFIRE && ds != DS_TEAM); @@ -534,8 +531,8 @@ static GMenu *create_clause_menu(GActionGroup *group, G_CALLBACK(diplomacy_dialog_peace_callback), pdialog); fc_snprintf(act_name, sizeof(act_name), "win.peace%s", act_plr_part); - item = g_menu_item_new(Q_("?diplomatic_state:Peace"), act_name); - g_menu_append_item(submenu, item); + menu_item_append_unref(submenu, g_menu_item_new(Q_("?diplomatic_state:Peace"), + act_name)); g_simple_action_set_enabled(G_SIMPLE_ACTION(act), ds != DS_PEACE && ds != DS_TEAM); @@ -550,8 +547,9 @@ static GMenu *create_clause_menu(GActionGroup *group, G_CALLBACK(diplomacy_dialog_alliance_callback), pdialog); fc_snprintf(act_name, sizeof(act_name), "win.alliance%s", act_plr_part); - item = g_menu_item_new(Q_("?diplomatic_state:Alliance"), act_name); - g_menu_append_item(submenu, item); + menu_item_append_unref(submenu, + g_menu_item_new(Q_("?diplomatic_state:Alliance"), + act_name)); g_simple_action_set_enabled(G_SIMPLE_ACTION(act), ds != DS_ALLIANCE && ds != DS_TEAM); diff --git a/client/gui-gtk-4.0/gui_stuff.h b/client/gui-gtk-4.0/gui_stuff.h index fed96904e8..96d35cc11e 100644 --- a/client/gui-gtk-4.0/gui_stuff.h +++ b/client/gui-gtk-4.0/gui_stuff.h @@ -139,4 +139,18 @@ gint blocking_dialog(GtkWidget *dlg); void widget_destroyed(GtkWidget *wdg, void *data); GtkWidget *widget_get_child(GtkWidget *wdg); -#endif /* FC__GUI_STUFF_H */ +#define menu_item_insert_unref(menu, index, item) \ +{ \ + GMenuItem *_item_var = item; \ + g_menu_insert_item(menu, index, _item_var); \ + g_object_unref(_item_var); \ +} + +#define menu_item_append_unref(menu, item) \ +{ \ + GMenuItem *_item_var = item; \ + g_menu_append_item(menu, _item_var); \ + g_object_unref(_item_var); \ +} + +#endif /* FC__GUI_STUFF_H */ diff --git a/client/gui-gtk-4.0/menu.c b/client/gui-gtk-4.0/menu.c index 3b9dd53463..e01af4a5b7 100644 --- a/client/gui-gtk-4.0/menu.c +++ b/client/gui-gtk-4.0/menu.c @@ -918,7 +918,6 @@ const GActionEntry acts[] = { static struct menu_entry_info *menu_entry_info_find(const char *key); - /************************************************************************//** Item "CLEAR_CHAT_LOGS" callback. ****************************************************************************/ @@ -992,8 +991,9 @@ static void save_options_on_exit_callback(GSimpleAction *action, gui_options.save_options_on_exit = info->state; g_menu_remove(options_menu, 4); - g_menu_insert_item(options_menu, 4, - create_toggle_menu_item_for_key("SAVE_OPTIONS_ON_EXIT")); + + menu_item_insert_unref(options_menu, 4, + create_toggle_menu_item_for_key("SAVE_OPTIONS_ON_EXIT")); } #ifdef MENUS_GTK3 @@ -1714,8 +1714,9 @@ static void full_screen_callback(GSimpleAction *action, GVariant *parameter, } g_menu_remove(view_menu, 0); - g_menu_insert_item(view_menu, 0, - create_toggle_menu_item_for_key("FULL_SCREEN")); + + menu_item_insert_unref(view_menu, 0, + create_toggle_menu_item_for_key("FULL_SCREEN")); } #ifdef MENUS_GTK3 @@ -2477,7 +2478,8 @@ static void bg_append_callback(GSimpleAction *action, } /************************************************************************//** - Create toggle menu entry by info + Create toggle menu entry by info. + Caller need to g_object_unref() returned item. ****************************************************************************/ static GMenuItem *create_toggle_menu_item(struct menu_entry_info *info) { @@ -2500,6 +2502,7 @@ static GMenuItem *create_toggle_menu_item(struct menu_entry_info *info) /************************************************************************//** Create toggle menu entry by key + Caller need to g_object_unref() returned item. ****************************************************************************/ static GMenuItem *create_toggle_menu_item_for_key(const char *key) { @@ -2526,6 +2529,7 @@ static void menu_entry_init(GMenu *sub, const char *key) } g_menu_append_item(sub, item); + g_object_unref(item); } } @@ -2974,7 +2978,6 @@ void real_menus_update(void) struct action *paction = action_by_number(act_id); GSimpleAction *act; char actname[256]; - GMenuItem *item; char name[256]; if (action_id_get_actor_kind(act_id) != AAK_UNIT) { @@ -3006,7 +3009,6 @@ void real_menus_update(void) #define CREATE_SUB_ITEM(_sub_target_, _sub_target_key_, _sub_target_name_) \ { \ - GMenuItem *sub_item; \ fc_snprintf(actname, sizeof(actname), "subtgt_%d", j); \ act = g_simple_action_new(actname, NULL); \ g_action_map_add_action(map, G_ACTION(act)); \ @@ -3015,8 +3017,8 @@ void real_menus_update(void) paction); \ fc_snprintf(subname, sizeof(subname), "%s", _sub_target_name_); \ fc_snprintf(actname, sizeof(actname), "app.subtgt_%d", j++); \ - sub_item = g_menu_item_new(subname, actname); \ - g_menu_append_item(sub_target_menu, sub_item); \ + menu_item_append_unref(sub_target_menu, \ + g_menu_item_new(subname, actname)); \ } switch (action_get_sub_target_kind(paction)) { @@ -3058,10 +3060,9 @@ void real_menus_update(void) g_menu_append_submenu(submenu, name, G_MENU_MODEL(sub_target_menu)); } else { - item = g_menu_item_new(name, actname); g_signal_connect(act, "activate", G_CALLBACK(unit_goto_and_callback), paction); - g_menu_append_item(submenu, item); + menu_item_append_unref(submenu, g_menu_item_new(name, actname)); } } action_iterate_end; } @@ -3074,7 +3075,6 @@ void real_menus_update(void) i = 0; governments_iterate(g) { if (g != game.government_during_revolution) { - GMenuItem *item; char name[256]; char actname[256]; GSimpleAction *act; @@ -3089,8 +3089,7 @@ void real_menus_update(void) fc_snprintf(name, sizeof(name), _("%s..."), government_name_translation(g)); fc_snprintf(actname, sizeof(actname), "app.government_%d", i++); - item = g_menu_item_new(name, actname); - g_menu_append_item(submenu, item); + menu_item_append_unref(submenu, g_menu_item_new(name, actname)); } } governments_iterate_end; g_menu_remove(gov_menu, 6); @@ -3100,7 +3099,6 @@ void real_menus_update(void) extra_type_by_cause_iterate(EC_ROAD, pextra) { if (pextra->buildable) { - GMenuItem *item; char actname[256]; GSimpleAction *act; @@ -3114,8 +3112,8 @@ void real_menus_update(void) g_signal_connect(act, "activate", G_CALLBACK(road_callback), pextra); fc_snprintf(actname, sizeof(actname), "app.path_%d", i++); - item = g_menu_item_new(extra_name_translation(pextra), actname); - g_menu_append_item(submenu, item); + menu_item_append_unref(submenu, + g_menu_item_new(extra_name_translation(pextra), actname)); } } extra_type_by_cause_iterate_end; @@ -3126,7 +3124,6 @@ void real_menus_update(void) extra_type_by_cause_iterate(EC_IRRIGATION, pextra) { if (pextra->buildable) { - GMenuItem *item; char actname[256]; GSimpleAction *act; @@ -3140,8 +3137,8 @@ void real_menus_update(void) g_signal_connect(act, "activate", G_CALLBACK(irrigation_callback), pextra); fc_snprintf(actname, sizeof(actname), "app.irrig_%d", i++); - item = g_menu_item_new(extra_name_translation(pextra), actname); - g_menu_append_item(submenu, item); + menu_item_append_unref(submenu, + g_menu_item_new(extra_name_translation(pextra), actname)); } } extra_type_by_cause_iterate_end; @@ -3152,7 +3149,6 @@ void real_menus_update(void) extra_type_by_cause_iterate(EC_MINE, pextra) { if (pextra->buildable) { - GMenuItem *item; char actname[256]; GSimpleAction *act; @@ -3166,8 +3162,8 @@ void real_menus_update(void) g_signal_connect(act, "activate", G_CALLBACK(mine_callback), pextra); fc_snprintf(actname, sizeof(actname), "app.mine_%d", i++); - item = g_menu_item_new(extra_name_translation(pextra), actname); - g_menu_append_item(submenu, item); + menu_item_append_unref(submenu, + g_menu_item_new(extra_name_translation(pextra), actname)); } } extra_type_by_cause_iterate_end; @@ -3177,7 +3173,6 @@ void real_menus_update(void) submenu = g_menu_new(); extra_type_by_rmcause_iterate(ERM_CLEANPOLLUTION, pextra) { - GMenuItem *item; char actname[256]; GSimpleAction *act; @@ -3191,12 +3186,11 @@ void real_menus_update(void) g_signal_connect(act, "activate", G_CALLBACK(clean_menu_callback), pextra); fc_snprintf(actname, sizeof(actname), "app.clean_%d", i++); - item = g_menu_item_new(extra_name_translation(pextra), actname); - g_menu_append_item(submenu, item); + menu_item_append_unref(submenu, + g_menu_item_new(extra_name_translation(pextra), actname)); } extra_type_by_rmcause_iterate_end; extra_type_by_rmcause_iterate(ERM_CLEANFALLOUT, pextra) { - GMenuItem *item; char actname[256]; GSimpleAction *act; @@ -3210,8 +3204,8 @@ void real_menus_update(void) g_signal_connect(act, "activate", G_CALLBACK(clean_menu_callback), pextra); fc_snprintf(actname, sizeof(actname), "app.clean_%d", i++); - item = g_menu_item_new(extra_name_translation(pextra), actname); - g_menu_append_item(submenu, item); + menu_item_append_unref(submenu, + g_menu_item_new(extra_name_translation(pextra), actname)); } extra_type_by_rmcause_iterate_end; g_menu_remove(work_menu, 5); @@ -3222,7 +3216,6 @@ void real_menus_update(void) extra_type_by_cause_iterate(EC_BASE, pextra) { if (pextra->buildable) { - GMenuItem *item; char actname[256]; GSimpleAction *act; @@ -3236,8 +3229,8 @@ void real_menus_update(void) g_signal_connect(act, "activate", G_CALLBACK(base_callback), pextra); fc_snprintf(actname, sizeof(actname), "app.base_%d", i++); - item = g_menu_item_new(extra_name_translation(pextra), actname); - g_menu_append_item(submenu, item); + menu_item_append_unref(submenu, + g_menu_item_new(extra_name_translation(pextra), actname)); } } extra_type_by_cause_iterate_end; diff --git a/client/gui-gtk-4.0/optiondlg.c b/client/gui-gtk-4.0/optiondlg.c index b0c12fb522..a85f2d24af 100644 --- a/client/gui-gtk-4.0/optiondlg.c +++ b/client/gui-gtk-4.0/optiondlg.c @@ -204,7 +204,6 @@ static gboolean option_button_press_callback(GtkGestureClick *gesture, GMenu *menu; GActionGroup *group; GSimpleAction *act; - GMenuItem *item; GdkRectangle rect = { .x = x, .y = y, .width = 1, .height = 1}; if (!option_is_changeable(poption)) { @@ -218,21 +217,21 @@ static gboolean option_button_press_callback(GtkGestureClick *gesture, act = g_simple_action_new("refresh", NULL); g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); g_signal_connect(act, "activate", G_CALLBACK(option_refresh_callback), poption); - item = g_menu_item_new(_("Refresh this option"), "win.refresh"); - g_menu_append_item(menu, item); + menu_item_append_unref(menu, g_menu_item_new(_("Refresh this option"), + "win.refresh")); act = g_simple_action_new("unit_reset", NULL); g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); g_signal_connect(act, "activate", G_CALLBACK(option_reset_callback), poption); - item = g_menu_item_new(_("Reset this option"), "win.unit_reset"); - g_menu_append_item(menu, item); + menu_item_append_unref(menu, g_menu_item_new(_("Reset this option"), + "win.unit_reset")); act = g_simple_action_new("units_apply", NULL); g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); g_signal_connect(act, "activate", G_CALLBACK(option_apply_callback), poption); - item = g_menu_item_new(_("Apply the changes for this option"), "win.units_apply"); - g_menu_append_item(menu, item); - + menu_item_append_unref(menu, + g_menu_item_new(_("Apply the changes for this option"), + "win.units_apply")); opt_popover = gtk_popover_menu_new_from_model(G_MENU_MODEL(menu)); g_object_ref(opt_popover); diff --git a/client/gui-gtk-4.0/pages.c b/client/gui-gtk-4.0/pages.c index b01646cc12..30bd8381a6 100644 --- a/client/gui-gtk-4.0/pages.c +++ b/client/gui-gtk-4.0/pages.c @@ -1871,7 +1871,6 @@ static GtkWidget *create_conn_menu(struct player *pplayer, GMenu *menu; gchar *buf; GSimpleAction *act; - GMenuItem *item; GActionGroup *group; group = G_ACTION_GROUP(g_simple_action_group_new()); @@ -1886,16 +1885,15 @@ static GtkWidget *create_conn_menu(struct player *pplayer, act = g_simple_action_new("info", NULL); g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); g_signal_connect(act, "activate", G_CALLBACK(conn_menu_info_chosen), menu); - item = g_menu_item_new(buf, "win.info"); - g_menu_append_item(menu, item); + menu_item_append_unref(menu, g_menu_item_new(buf, "win.info")); if (NULL != pplayer) { act = g_simple_action_new("toggle_ready", NULL); g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); g_signal_connect(act, "activate", G_CALLBACK(conn_menu_ready_chosen), menu); g_simple_action_set_enabled(G_SIMPLE_ACTION(act), is_human(pplayer)); - item = g_menu_item_new(_("Toggle player ready"), "win.toggle_ready"); - g_menu_append_item(menu, item); + menu_item_append_unref(menu, g_menu_item_new(_("Toggle player ready"), + "win.toggle_ready")); act = g_simple_action_new("pick_nation", NULL); g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); @@ -1903,22 +1901,21 @@ static GtkWidget *create_conn_menu(struct player *pplayer, g_simple_action_set_enabled(G_SIMPLE_ACTION(act), can_conn_edit_players_nation(&client.conn, pplayer)); - item = g_menu_item_new(_("Pick nation"), "win.pick_nation"); - g_menu_append_item(menu, item); + menu_item_append_unref(menu, g_menu_item_new(_("Pick nation"), "win.pick_nation")); act = g_simple_action_new("observe", NULL); g_object_set_data_full(G_OBJECT(act), "command", g_strdup("observe"), (GDestroyNotify) g_free); g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); g_signal_connect(act, "activate", G_CALLBACK(conn_menu_player_command), menu); - item = g_menu_item_new(_("Observe this player"), "win.observe"); - g_menu_append_item(menu, item); + menu_item_append_unref(menu, g_menu_item_new(_("Observe this player"), + "win.observe")); act = g_simple_action_new("take_plr", NULL); g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); g_signal_connect(act, "activate", G_CALLBACK(conn_menu_player_take), menu); - item = g_menu_item_new(_("Take this player"), "win.take_plr"); - g_menu_append_item(menu, item); + menu_item_append_unref(menu, g_menu_item_new(_("Take this player"), + "win.take_plr")); } if (ALLOW_CTRL <= client.conn.access_level && NULL != pconn @@ -1928,8 +1925,7 @@ static GtkWidget *create_conn_menu(struct player *pplayer, (GDestroyNotify) g_free); g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); g_signal_connect(act, "activate", G_CALLBACK(conn_menu_connection_command), menu); - item = g_menu_item_new(_("Cut connection"), "win.cut_conn"); - g_menu_append_item(menu, item); + menu_item_append_unref(menu, g_menu_item_new(_("Cut connection"), "win.cut_conn")); } if (ALLOW_CTRL <= client.conn.access_level && NULL != pplayer) { @@ -1938,8 +1934,7 @@ static GtkWidget *create_conn_menu(struct player *pplayer, (GDestroyNotify) g_free); g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); g_signal_connect(act, "activate", G_CALLBACK(conn_menu_connection_command), menu); - item = g_menu_item_new(_("Aitoggle player"), "win.aitoggle"); - g_menu_append_item(menu, item); + menu_item_append_unref(menu, g_menu_item_new(_("Aitoggle player"), "win.aitoggle")); if (pplayer != client.conn.playing && game.info.is_new_game) { act = g_simple_action_new("remove", NULL); @@ -1947,8 +1942,7 @@ static GtkWidget *create_conn_menu(struct player *pplayer, (GDestroyNotify) g_free); g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); g_signal_connect(act, "activate", G_CALLBACK(conn_menu_connection_command), menu); - item = g_menu_item_new(_("Remove player"), "win.remove"); - g_menu_append_item(menu, item); + menu_item_append_unref(menu, g_menu_item_new(_("Remove player"), "win.remove")); } } @@ -1970,10 +1964,9 @@ static GtkWidget *create_conn_menu(struct player *pplayer, (GDestroyNotify) g_free); g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); g_signal_connect(act, "activate", G_CALLBACK(conn_menu_connection_command), menu); - fc_snprintf(actbuf, sizeof(actbuf), "win.cmdlevel_%d", level); - item = g_menu_item_new(buf, actbuf); + fc_snprintf(actbuf, sizeof(actbuf), "win.cmdlevel_%d", level); + menu_item_append_unref(menu, g_menu_item_new(buf, actbuf)); g_free(buf); - g_menu_append_item(menu, item); } } @@ -1995,9 +1988,8 @@ static GtkWidget *create_conn_menu(struct player *pplayer, g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); g_signal_connect(act, "activate", G_CALLBACK(conn_menu_player_command), menu); fc_snprintf(actbuf, sizeof(actbuf), "win.ailevel_%d", level); - item = g_menu_item_new(buf, actbuf); + menu_item_append_unref(menu, g_menu_item_new(buf, actbuf)); g_free(buf); - g_menu_append_item(menu, item); } } } @@ -2031,9 +2023,8 @@ static GtkWidget *create_conn_menu(struct player *pplayer, g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); g_signal_connect(act, "activate", G_CALLBACK(conn_menu_team_chosen), menu); fc_snprintf(actbuf, sizeof(actbuf), "win.team_%d", id); - item = g_menu_item_new(buf, actbuf); + menu_item_append_unref(menu, g_menu_item_new(buf, actbuf)); g_free(buf); - g_menu_append_item(menu, item); } team_slots_iterate_end; } diff --git a/client/gui-gtk-4.0/plrdlg.c b/client/gui-gtk-4.0/plrdlg.c index 97718fa7d2..d3fb879f2b 100644 --- a/client/gui-gtk-4.0/plrdlg.c +++ b/client/gui-gtk-4.0/plrdlg.c @@ -352,7 +352,8 @@ static GtkListStore *players_dialog_store_new(void) } /************************************************************************//** - Create up-to-date menu item for the plrdlg display menu + Create up-to-date menu item for the plrdlg display menu. + Caller need to g_object_unref() returned item. ****************************************************************************/ static GMenuItem *create_plrdlg_display_menu_item(int pos) { @@ -371,7 +372,8 @@ static GMenuItem *create_plrdlg_display_menu_item(int pos) } /************************************************************************//** - Create up-to-date menu item for "Dead Players" menu entry + Create up-to-date menu item for "Dead Players" menu entry. + Caller need to g_object_unref() returned item. ****************************************************************************/ static GMenuItem *create_dead_players_menu_item(void) { @@ -402,7 +404,8 @@ static void toggle_view(GSimpleAction *act, GVariant *value, gpointer data) /* The menu has no 'playername' in the beginning, so menu index is one smaller * then column index. */ g_menu_remove(display_menu, idx - 1); - g_menu_insert_item(display_menu, idx - 1, create_plrdlg_display_menu_item(idx)); + + menu_item_insert_unref(display_menu, idx - 1, create_plrdlg_display_menu_item(idx)); } /**********************************************************************//** @@ -412,7 +415,6 @@ static void toggle_dead_players(GSimpleAction *act, GVariant *value, gpointer data) { int idx = GPOINTER_TO_INT(data); - GMenuItem *item; gui_options.player_dlg_show_dead_players ^= 1; real_players_dialog_update(NULL); @@ -422,9 +424,8 @@ static void toggle_dead_players(GSimpleAction *act, GVariant *value, * a column. */ g_menu_remove(display_menu, idx - 1); - item = create_dead_players_menu_item(); - g_menu_insert_item(display_menu, idx - 1, item); - g_object_unref(item); + menu_item_insert_unref(display_menu, idx - 1, + create_dead_players_menu_item()); } /**********************************************************************//** @@ -434,7 +435,6 @@ static void toggle_dead_players(GSimpleAction *act, GVariant *value, static GMenu *create_diplomacy_menu(GActionGroup *group) { GMenu *menu; - GMenuItem *item; GSimpleAction *act; menu = g_menu_new(); @@ -442,22 +442,21 @@ static GMenu *create_diplomacy_menu(GActionGroup *group) act = g_simple_action_new("meet", NULL); g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); g_signal_connect(act, "activate", G_CALLBACK(players_meet_callback), NULL); - item = g_menu_item_new(_("_Meet"), "win.meet"); - g_menu_append_item(menu, item); + menu_item_append_unref(menu, g_menu_item_new(_("_Meet"), "win.meet")); players_meet_command = act; act = g_simple_action_new("cancel_treaty", NULL); g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); g_signal_connect(act, "activate", G_CALLBACK(players_war_callback), NULL); - item = g_menu_item_new(_("Cancel _Treaty"), "win.cancel_treaty"); - g_menu_append_item(menu, item); + menu_item_append_unref(menu, g_menu_item_new(_("Cancel _Treaty"), + "win.cancel_treaty")); players_war_command = act; act = g_simple_action_new("withdraw_vision", NULL); g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); g_signal_connect(act, "activate", G_CALLBACK(players_vision_callback), NULL); - item = g_menu_item_new(_("_Withdraw Vision"), "win.withdraw_vision"); - g_menu_append_item(menu, item); + menu_item_append_unref(menu, g_menu_item_new(_("_Withdraw Vision"), + "win.withdraw_vision")); players_vision_command = act; return menu; @@ -470,7 +469,6 @@ static GMenu *create_diplomacy_menu(GActionGroup *group) static GMenu *create_intelligence_menu(GActionGroup *group) { GMenu *menu; - GMenuItem *item; GSimpleAction *act; menu = g_menu_new(); @@ -478,21 +476,18 @@ static GMenu *create_intelligence_menu(GActionGroup *group) act = g_simple_action_new("report", NULL); g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); g_signal_connect(act, "activate", G_CALLBACK(players_intel_callback), NULL); - item = g_menu_item_new(_("_Report"), "win.report"); - g_menu_append_item(menu, item); + menu_item_append_unref(menu, g_menu_item_new(_("_Report"), "win.report")); players_int_command = act; act = g_simple_action_new("wonders", NULL); g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); g_signal_connect(act, "activate", G_CALLBACK(players_intel_wonder_callback), NULL); - item = g_menu_item_new(_("_Wonders"), "win.wonders"); - g_menu_append_item(menu, item); + menu_item_append_unref(menu, g_menu_item_new(_("_Wonders"), "win.wonders")); act = g_simple_action_new("spaceship", NULL); g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); g_signal_connect(act, "activate", G_CALLBACK(players_sship_callback), NULL); - item = g_menu_item_new(_("_Spaceship"), "win.spaceship"); - g_menu_append_item(menu, item); + menu_item_append_unref(menu, g_menu_item_new(_("_Spaceship"), "win.spaceship")); players_sship_command = act; return menu; @@ -507,7 +502,6 @@ static GMenu *create_show_menu(GActionGroup *group) int i; GSimpleAction *act; GVariant *var; - GMenuItem *item; display_menu = g_menu_new(); @@ -521,18 +515,16 @@ static GMenu *create_show_menu(GActionGroup *group) g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); g_signal_connect(act, "change-state", G_CALLBACK(toggle_view), GINT_TO_POINTER(i)); - g_menu_insert_item(display_menu, i, create_plrdlg_display_menu_item(i)); + menu_item_insert_unref(display_menu, i, create_plrdlg_display_menu_item(i)); } var = g_variant_new("b", TRUE); act = g_simple_action_new_stateful("show_dead", bvart, var); g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); - item = create_dead_players_menu_item(); - g_menu_insert_item(display_menu, i, item); + menu_item_insert_unref(display_menu, i, create_dead_players_menu_item()); g_signal_connect(act, "change-state", G_CALLBACK(toggle_dead_players), GINT_TO_POINTER(i)); - g_object_unref(item); g_variant_type_free(bvart); @@ -546,7 +538,6 @@ static GMenu *create_show_menu(GActionGroup *group) static GMenu *create_ai_menu(GActionGroup *group) { GMenu *menu; - GMenuItem *item; GSimpleAction *act; enum ai_level level; @@ -557,8 +548,8 @@ static GMenu *create_ai_menu(GActionGroup *group) g_signal_connect(act, "activate", G_CALLBACK(players_ai_toggle_callback), NULL); - item = g_menu_item_new(_("_Toggle AI Mode"), "win.ai_toggle"); - g_menu_append_item(menu, item); + menu_item_append_unref(menu, g_menu_item_new(_("_Toggle AI Mode"), + "win.ai_toggle")); for (level = 0; level < AI_LEVEL_COUNT; level++) { if (is_settable_ai_level(level)) { @@ -573,8 +564,7 @@ static GMenu *create_ai_menu(GActionGroup *group) GUINT_TO_POINTER(level)); fc_snprintf(act_name, sizeof(act_name), "win.ai_level%d", level); - item = g_menu_item_new(level_name, act_name); - g_menu_append_item(menu, item); + menu_item_append_unref(menu, g_menu_item_new(level_name, act_name)); } } diff --git a/client/gui-gtk-4.0/wldlg.c b/client/gui-gtk-4.0/wldlg.c index 40752a497f..bc70272f08 100644 --- a/client/gui-gtk-4.0/wldlg.c +++ b/client/gui-gtk-4.0/wldlg.c @@ -486,7 +486,6 @@ static void menu_item_callback(GSimpleAction *action, GVariant *parameter, ****************************************************************************/ static GMenu *create_wl_menu(struct worklist_data *ptr) { - GMenuItem *item; GSimpleAction *act; int current_size = 0; @@ -508,12 +507,12 @@ static GMenu *create_wl_menu(struct worklist_data *ptr) g_signal_connect(act, "activate", G_CALLBACK(menu_item_callback), ptr); fc_snprintf(act_name, sizeof(act_name), "win.wl%d", id); - item = g_menu_item_new(global_worklist_name(pgwl), act_name); if (ptr->menu_size > current_size) { g_menu_remove(ptr->menu, current_size); } - g_menu_insert_item(ptr->menu, current_size++, item); + menu_item_insert_unref(ptr->menu, current_size++, + g_menu_item_new(global_worklist_name(pgwl), act_name)); } global_worklists_iterate_end; act = g_simple_action_new("wledit", NULL); @@ -521,11 +520,12 @@ static GMenu *create_wl_menu(struct worklist_data *ptr) g_signal_connect(act, "activate", G_CALLBACK(popup_worklists_report), NULL); - item = g_menu_item_new(_("Edit Global _Worklists"), "win.wledit"); if (ptr->menu_size > current_size) { g_menu_remove(ptr->menu, current_size); } - g_menu_insert_item(ptr->menu, current_size++, item); + + menu_item_insert_unref(ptr->menu, current_size++, + g_menu_item_new(_("Edit Global _Worklists"), "win.wledit")); if (ptr->menu_size < current_size) { ptr->menu_size = current_size; -- 2.39.0