[gspell] init: add a destructor to unref singleton



commit 12946267bcb1c62f640a590f55b05287ebd4661b
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sun Feb 28 14:04:54 2016 +0100

    init: add a destructor to unref singleton
    
    https://bugzilla.gnome.org/show_bug.cgi?id=761925

 gspell/gspell-buffer-notifier.c |    4 ++++
 gspell/gspell-init.c            |   27 +++++++++++++++++++++++++++
 2 files changed, 31 insertions(+), 0 deletions(-)
---
diff --git a/gspell/gspell-buffer-notifier.c b/gspell/gspell-buffer-notifier.c
index ed16115..09c4d77 100644
--- a/gspell/gspell-buffer-notifier.c
+++ b/gspell/gspell-buffer-notifier.c
@@ -72,6 +72,10 @@ _gspell_buffer_notifier_get_instance (void)
        if (instance == NULL)
        {
                instance = g_object_new (GSPELL_TYPE_BUFFER_NOTIFIER, NULL);
+
+               /* In case the caller unrefs the instance. */
+               g_object_add_weak_pointer (G_OBJECT (instance),
+                                          (gpointer *) &instance);
        }
 
        return instance;
diff --git a/gspell/gspell-init.c b/gspell/gspell-init.c
index 0631cdb..8c8b006 100644
--- a/gspell/gspell-init.c
+++ b/gspell/gspell-init.c
@@ -27,6 +27,7 @@
 #include <glib.h>
 #include <glib/gi18n-lib.h>
 #include "gconstructor.h"
+#include "gspell-buffer-notifier.h"
 
 #ifdef G_OS_WIN32
 #define WIN32_LEAN_AND_MEAN
@@ -122,6 +123,18 @@ gspell_init (void)
        bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
 }
 
+static void
+gspell_finalize (void)
+{
+       GspellBufferNotifier *notifier;
+
+       /* Destroy singleton, to have less garbage in the output of memory
+        * debugging tools.
+        */
+       notifier = _gspell_buffer_notifier_get_instance ();
+       g_object_unref (notifier);
+}
+
 #if defined (G_OS_WIN32)
 
 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
@@ -141,6 +154,9 @@ DllMain (HINSTANCE hinstDLL,
                        break;
 
                case DLL_THREAD_DETACH:
+                       gspell_finalize ();
+                       break;
+
                default:
                        /* do nothing */
                        break;
@@ -162,6 +178,17 @@ gspell_constructor (void)
        gspell_init ();
 }
 
+#  ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
+#    pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(gspell_destructor)
+#  endif
+G_DEFINE_DESTRUCTOR (gspell_destructor)
+
+static void
+gspell_destructor (void)
+{
+       gspell_finalize ();
+}
+
 #else
 #  error Your platform/compiler is missing constructor support
 #endif


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]