From 2d45d41b2bae93062d76d0dc48a025b348631801 Mon Sep 17 00:00:00 2001 From: John Darrington Date: Sat, 4 Jul 2020 11:30:29 +0200 Subject: [PATCH] Audit and cleanup dispose methods of classes which have them. Ensure that all non-trivial dispose methods use the dispose_has_run flag and that only unreffing takes place within dispose. --- src/ui/gui/psppire-acr.c | 7 ++++++ src/ui/gui/psppire-acr.h | 2 +- src/ui/gui/psppire-conf.c | 2 -- src/ui/gui/psppire-conf.h | 1 - src/ui/gui/psppire-data-editor.c | 26 +++++++++------------ src/ui/gui/psppire-data-editor.h | 2 ++ src/ui/gui/psppire-data-window.c | 35 +++++++++++++--------------- src/ui/gui/psppire-data-window.h | 2 ++ src/ui/gui/psppire-dict.c | 13 ++++++++--- src/ui/gui/psppire-dict.h | 2 ++ src/ui/gui/psppire-scanf.c | 2 ++ src/ui/gui/psppire-window-register.c | 1 - src/ui/gui/psppire-window-register.h | 1 - 13 files changed, 53 insertions(+), 43 deletions(-) diff --git a/src/ui/gui/psppire-acr.c b/src/ui/gui/psppire-acr.c index b858df268e..a876413fd5 100644 --- a/src/ui/gui/psppire-acr.c +++ b/src/ui/gui/psppire-acr.c @@ -50,6 +50,11 @@ static void psppire_acr_dispose (GObject *obj) { PsppireAcr *acr = PSPPIRE_ACR (obj); + + if (acr->dispose_has_run) + return; + acr->dispose_has_run = TRUE; + psppire_acr_set_model (acr, NULL); G_OBJECT_CLASS (psppire_acr_parent_class)->dispose (obj); @@ -259,6 +264,8 @@ psppire_acr_init (PsppireAcr *acr) GtkWidget *sw = gtk_scrolled_window_new (NULL, NULL); + acr->dispose_has_run = FALSE; + gtk_orientable_set_orientation (GTK_ORIENTABLE (acr), GTK_ORIENTATION_HORIZONTAL); acr->tv = GTK_TREE_VIEW (gtk_tree_view_new ()); diff --git a/src/ui/gui/psppire-acr.h b/src/ui/gui/psppire-acr.h index 73873df356..c38f3f5f3a 100644 --- a/src/ui/gui/psppire-acr.h +++ b/src/ui/gui/psppire-acr.h @@ -66,7 +66,7 @@ struct _PsppireAcr { GtkBox parent; - + gboolean dispose_has_run; GtkListStore *list_store; GtkTreeView *tv; diff --git a/src/ui/gui/psppire-conf.c b/src/ui/gui/psppire-conf.c index 2629fdcfc0..835d58c127 100644 --- a/src/ui/gui/psppire-conf.c +++ b/src/ui/gui/psppire-conf.c @@ -148,7 +148,6 @@ psppire_conf_class_init (PsppireConfClass *class) object_class->finalize = psppire_conf_finalize; object_class->dispose = psppire_conf_dispose; object_class->constructor = psppire_conf_construct; - } @@ -169,7 +168,6 @@ psppire_conf_init (PsppireConf *conf) conf->keyfile = g_key_file_new (); - conf->dispose_has_run = FALSE; conf->idle = 0; } diff --git a/src/ui/gui/psppire-conf.h b/src/ui/gui/psppire-conf.h index 5f4b283af5..de8db9397d 100644 --- a/src/ui/gui/psppire-conf.h +++ b/src/ui/gui/psppire-conf.h @@ -58,7 +58,6 @@ struct _PsppireConf GObject parent; /*< private >*/ - gboolean dispose_has_run ; GKeyFile *keyfile; gchar *filename; diff --git a/src/ui/gui/psppire-data-editor.c b/src/ui/gui/psppire-data-editor.c index 181e5cbf3d..e852b7d92b 100644 --- a/src/ui/gui/psppire-data-editor.c +++ b/src/ui/gui/psppire-data-editor.c @@ -83,6 +83,10 @@ static GObjectClass * parent_class = NULL; static void psppire_data_editor_finalize (GObject *obj) { + PsppireDataEditor *de = (PsppireDataEditor *) obj; + if (de->font) + pango_font_description_free (de->font); + /* Chain up to the parent class */ G_OBJECT_CLASS (parent_class)->finalize (obj); } @@ -92,23 +96,13 @@ psppire_data_editor_dispose (GObject *obj) { PsppireDataEditor *de = (PsppireDataEditor *) obj; - if (de->data_store) - { - g_object_unref (de->data_store); - de->data_store = NULL; - } + if (de->dispose_has_run) + return; - if (de->dict) - { - g_object_unref (de->dict); - de->dict = NULL; - } + de->dispose_has_run = TRUE; - if (de->font != NULL) - { - pango_font_description_free (de->font); - de->font = NULL; - } + g_object_unref (de->data_store); + g_object_unref (de->dict); /* Chain up to the parent class */ G_OBJECT_CLASS (parent_class)->dispose (obj); @@ -491,6 +485,8 @@ psppire_data_editor_init (PsppireDataEditor *de) GtkStyleContext *context = gtk_widget_get_style_context (GTK_WIDGET (de)); gtk_style_context_add_class (context, "psppire-data-editor"); + de->dispose_has_run = FALSE; + de->font = NULL; g_object_set (de, "tab-pos", GTK_POS_BOTTOM, NULL); diff --git a/src/ui/gui/psppire-data-editor.h b/src/ui/gui/psppire-data-editor.h index 88d07a8114..a5152bb759 100644 --- a/src/ui/gui/psppire-data-editor.h +++ b/src/ui/gui/psppire-data-editor.h @@ -51,6 +51,8 @@ struct _PsppireDataEditor { GtkNotebook parent; + gboolean dispose_has_run; + /* */ PsppireDataStore *data_store; PsppireDict *dict; diff --git a/src/ui/gui/psppire-data-window.c b/src/ui/gui/psppire-data-window.c index 2fffd790a5..811d5e9eb8 100644 --- a/src/ui/gui/psppire-data-window.c +++ b/src/ui/gui/psppire-data-window.c @@ -958,9 +958,11 @@ enable_save (PsppireDataWindow *dw) modify the menu as part of the "filename" property_set() function and end up with a Gtk-CRITICAL since 'menu' is NULL. */ static void -psppire_data_window_init (PsppireDataWindow *de) +psppire_data_window_init (PsppireDataWindow *dw) { - de->builder = builder_new ("data-editor.ui"); + dw->dispose_has_run = FALSE; + + dw->builder = builder_new ("data-editor.ui"); } static void @@ -1803,11 +1805,11 @@ psppire_data_window_dispose (GObject *object) { PsppireDataWindow *dw = PSPPIRE_DATA_WINDOW (object); - if (dw->builder != NULL) - { - g_object_unref (dw->builder); - dw->builder = NULL; - } + if (dw->dispose_has_run) + return; + dw->dispose_has_run = TRUE; + + g_object_unref (dw->builder); if (dw->dict) { @@ -1821,20 +1823,9 @@ psppire_data_window_dispose (GObject *object) G_CALLBACK (on_split_change), dw); g_object_unref (dw->dict); - dw->dict = NULL; } - if (dw->data_store) - { - g_object_unref (dw->data_store); - dw->data_store = NULL; - } - - if (dw->ll.next != NULL) - { - ll_remove (&dw->ll); - dw->ll.next = NULL; - } + g_object_unref (dw->data_store); if (G_OBJECT_CLASS (parent_class)->dispose) G_OBJECT_CLASS (parent_class)->dispose (object); @@ -1857,6 +1848,12 @@ psppire_data_window_finalize (GObject *object) dataset_destroy (dataset); } + if (dw->ll.next != NULL) + { + ll_remove (&dw->ll); + dw->ll.next = NULL; + } + if (G_OBJECT_CLASS (parent_class)->finalize) G_OBJECT_CLASS (parent_class)->finalize (object); } diff --git a/src/ui/gui/psppire-data-window.h b/src/ui/gui/psppire-data-window.h index a632c8d112..61ff409ac7 100644 --- a/src/ui/gui/psppire-data-window.h +++ b/src/ui/gui/psppire-data-window.h @@ -62,6 +62,8 @@ struct _PsppireDataWindow { PsppireWindow parent; + gboolean dispose_has_run; + /* */ PsppireDataEditor *data_editor; GtkBuilder *builder; diff --git a/src/ui/gui/psppire-dict.c b/src/ui/gui/psppire-dict.c index 87d13bc6e4..e381e5dab7 100644 --- a/src/ui/gui/psppire-dict.c +++ b/src/ui/gui/psppire-dict.c @@ -287,6 +287,11 @@ psppire_dict_dispose (GObject *object) { PsppireDict *d = PSPPIRE_DICT (object); + if (!d->dispose_has_run) + return; + + d->dispose_has_run = TRUE; + dict_set_callbacks (d->dict, NULL, NULL); dict_unref (d->dict); @@ -352,10 +357,12 @@ static const struct dict_callbacks gui_callbacks = }; static void -psppire_dict_init (PsppireDict *psppire_dict) +psppire_dict_init (PsppireDict *d) { - psppire_dict->stamp = g_random_int (); - psppire_dict->disable_insert_signal = FALSE; + d->dispose_has_run = FALSE; + + d->stamp = g_random_int (); + d->disable_insert_signal = FALSE; } /** diff --git a/src/ui/gui/psppire-dict.h b/src/ui/gui/psppire-dict.h index 2c88a49492..c3674b1330 100644 --- a/src/ui/gui/psppire-dict.h +++ b/src/ui/gui/psppire-dict.h @@ -61,6 +61,8 @@ struct _PsppireDict GObject parent; struct dictionary *dict; + gboolean dispose_has_run; + gboolean disable_insert_signal; /* For GtkTreeModelIface */ gint stamp; diff --git a/src/ui/gui/psppire-scanf.c b/src/ui/gui/psppire-scanf.c index 951b27357f..e91b03ee1d 100644 --- a/src/ui/gui/psppire-scanf.c +++ b/src/ui/gui/psppire-scanf.c @@ -331,6 +331,8 @@ psppire_scanf_class_init (PsppireScanfClass *class) static void psppire_scanf_init (PsppireScanf *w) { + w->dispose_has_run = FALSE; + gtk_orientable_set_orientation (GTK_ORIENTABLE (w), GTK_ORIENTATION_HORIZONTAL); } diff --git a/src/ui/gui/psppire-window-register.c b/src/ui/gui/psppire-window-register.c index 6bb2b7cb2f..66e7353fb1 100644 --- a/src/ui/gui/psppire-window-register.c +++ b/src/ui/gui/psppire-window-register.c @@ -134,7 +134,6 @@ psppire_window_register_class_init (PsppireWindowRegisterClass *class) static void psppire_window_register_init (PsppireWindowRegister *window_register) { - window_register->dispose_has_run = FALSE; window_register->name_table = g_hash_table_new (g_str_hash, g_str_equal); } diff --git a/src/ui/gui/psppire-window-register.h b/src/ui/gui/psppire-window-register.h index cf4f4d8ec9..fdc6551926 100644 --- a/src/ui/gui/psppire-window-register.h +++ b/src/ui/gui/psppire-window-register.h @@ -58,7 +58,6 @@ struct _PsppireWindowRegister GObject parent; /*< private >*/ - gboolean dispose_has_run ; GHashTable *name_table; }; -- 2.30.2