From f5d8dd4479d4bd7c73a85a36a619120da3047d4a Mon Sep 17 00:00:00 2001 From: John Darrington Date: Sat, 27 Jun 2020 07:27:25 +0200 Subject: [PATCH] PSPPIRE: Avoid memory leaks from popup memnus --- src/ui/gui/psppire-data-sheet.c | 15 ++++++++++++--- src/ui/gui/psppire-variable-sheet.c | 6 +++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/ui/gui/psppire-data-sheet.c b/src/ui/gui/psppire-data-sheet.c index 83bc892e15..c5ea4b644a 100644 --- a/src/ui/gui/psppire-data-sheet.c +++ b/src/ui/gui/psppire-data-sheet.c @@ -163,6 +163,10 @@ create_data_row_header_popup_menu (PsppireDataSheet *sheet) { GtkWidget *menu = gtk_menu_new (); + /* gtk_menu_shell_append does not sink/ref this object, + so we must do it ourselves (and remember to unref it). */ + g_object_ref_sink (menu); + GtkWidget *item = gtk_menu_item_new_with_mnemonic (_("_Insert Case")); @@ -288,13 +292,15 @@ psppire_data_sheet_delete_variables (PsppireDataSheet *sheet) gtk_widget_queue_draw (GTK_WIDGET (sheet)); } - - static GtkWidget * create_data_column_header_popup_menu (PsppireDataSheet *sheet) { GtkWidget *menu = gtk_menu_new (); + /* gtk_menu_shell_append does not sink/ref this object, + so we must do it ourselves (and remember to unref it). */ + g_object_ref_sink (menu); + GtkWidget *item = gtk_menu_item_new_with_mnemonic (_("_Insert Variable")); g_signal_connect_swapped (item, "activate", G_CALLBACK (insert_new_variable), @@ -345,13 +351,16 @@ static gboolean dispose_has_run = FALSE; static void psppire_data_sheet_dispose (GObject *obj) { - // PsppireDataSheet *sheet = PSPPIRE_DATA_SHEET (obj); + PsppireDataSheet *sheet = PSPPIRE_DATA_SHEET (obj); if (dispose_has_run) return; dispose_has_run = TRUE; + g_object_unref (sheet->data_sheet_cases_column_popup); + g_object_unref (sheet->data_sheet_cases_row_popup); + /* Chain up to the parent class */ G_OBJECT_CLASS (parent_class)->dispose (obj); } diff --git a/src/ui/gui/psppire-variable-sheet.c b/src/ui/gui/psppire-variable-sheet.c index 149403743a..05c3aaf5da 100644 --- a/src/ui/gui/psppire-variable-sheet.c +++ b/src/ui/gui/psppire-variable-sheet.c @@ -266,6 +266,10 @@ create_var_row_header_popup_menu (PsppireVariableSheet *var_sheet) { GtkWidget *menu = gtk_menu_new (); + /* gtk_menu_shell_append does not sink/ref this object, + so we must do it ourselves (and remember to unref it). */ + g_object_ref_sink (menu); + GtkWidget *item = gtk_menu_item_new_with_mnemonic (_("_Insert Variable")); g_signal_connect_swapped (item, "activate", G_CALLBACK (insert_new_variable_var), @@ -432,6 +436,7 @@ psppire_variable_sheet_dispose (GObject *obj) g_object_unref (sheet->value_label_renderer); g_object_unref (sheet->missing_values_renderer); g_object_unref (sheet->var_type_renderer); + g_object_unref (sheet->row_popup); /* Chain up to the parent class */ G_OBJECT_CLASS (parent_class)->dispose (obj); @@ -593,7 +598,6 @@ psppire_variable_sheet_init (PsppireVariableSheet *sheet) sheet->row_popup = create_var_row_header_popup_menu (sheet); - g_signal_connect (sheet, "selection-changed", G_CALLBACK (set_var_popup_sensitivity), sheet); -- 2.30.2