[devhelp] Add dh_free_resources() public function



commit 09e95b02aa6d076d7b20339183081327e32140db
Author: Sébastien Wilmet <sebastien wilmet uclouvain be>
Date:   Wed May 3 19:02:45 2017 +0200

    Add dh_free_resources() public function
    
    There is already a dh_init() function, so it makes sense to add the
    symmetric.
    
    Put it in dh-init.c. "init" is not really a great filename since it's
    both for initialization and de-initialization, but I have trouble to
    find a better name. dh-common.c was not great either in my opinion.

 docs/reference/devhelp-sections.txt |    3 ++-
 src/dh-init.c                       |   34 ++++++++++++++++++++++++++++++++++
 src/dh-init.h                       |    3 ++-
 src/dh-main.c                       |    6 ++----
 4 files changed, 40 insertions(+), 6 deletions(-)
---
diff --git a/docs/reference/devhelp-sections.txt b/docs/reference/devhelp-sections.txt
index 9d7ea22..17ec63e 100644
--- a/docs/reference/devhelp-sections.txt
+++ b/docs/reference/devhelp-sections.txt
@@ -1,7 +1,8 @@
 <SECTION>
 <FILE>init</FILE>
-<TITLE>Initialization</TITLE>
+<TITLE>Initialization and De-initialization</TITLE>
 dh_init
+dh_free_resources
 </SECTION>
 
 <SECTION>
diff --git a/src/dh-init.c b/src/dh-init.c
index d2a5b63..a6f84f1 100644
--- a/src/dh-init.c
+++ b/src/dh-init.c
@@ -1,6 +1,7 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
 /*
  * Copyright (C) 2012 Aleksander Morgado <aleksander gnu org>
+ * Copyright (C) 2017 Sébastien Wilmet <swilmet gnome org>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
@@ -18,8 +19,41 @@
 
 #include "config.h"
 #include "dh-init.h"
+#include "dh-book-manager.h"
+#include "dh-settings.h"
 
 void
 dh_init (void)
 {
 }
+
+/**
+ * dh_free_resources:
+ *
+ * Free the resources allocated by Devhelp. For example it unrefs the singleton
+ * objects.
+ *
+ * It is not mandatory to call this function, it's just to be friendlier to
+ * memory debugging tools. This function is meant to be called at the end of
+ * main().
+ *
+ * Since: 3.26
+ */
+
+/* Another way is to use a DSO destructor, see gconstructor.h in GLib.
+ *
+ * The advantage of calling dh_free_resources() at the end of main() is that
+ * gobject-list [1] correctly reports that all Dh* objects have been finalized
+ * when quitting the application. On the other hand a DSO destructor runs after
+ * the gobject-list's last output, so it's much less convenient, see:
+ * https://git.gnome.org/browse/gtksourceview/commit/?id=e761de9c2bee90c232875bbc41e6e73e1f63e145
+ *
+ * [1] A tool for debugging the lifetime of GObjects:
+ * https://github.com/danni/gobject-list
+ */
+void
+dh_free_resources (void)
+{
+        dh_book_manager_free_singleton ();
+        dh_settings_free_singleton ();
+}
diff --git a/src/dh-init.h b/src/dh-init.h
index 71ca74f..6770787 100644
--- a/src/dh-init.h
+++ b/src/dh-init.h
@@ -23,7 +23,8 @@
 
 G_BEGIN_DECLS
 
-void    dh_init         (void);
+void    dh_init                 (void);
+void    dh_free_resources       (void);
 
 G_END_DECLS
 
diff --git a/src/dh-main.c b/src/dh-main.c
index 4e5709f..4e75af7 100644
--- a/src/dh-main.c
+++ b/src/dh-main.c
@@ -23,9 +23,8 @@
 #include <glib/gi18n-lib.h>
 #include <gtk/gtk.h>
 
+#include "devhelp.h"
 #include "dh-app.h"
-#include "dh-book-manager.h"
-#include "dh-settings.h"
 
 int
 main (int argc, char **argv)
@@ -46,8 +45,7 @@ main (int argc, char **argv)
 
         g_object_unref (application);
 
-        dh_book_manager_free_singleton ();
-        dh_settings_free_singleton ();
+        dh_free_resources ();
 
         return status;
 }


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