[gtksourceview] Use a DSO destructor
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtksourceview] Use a DSO destructor
- Date: Tue, 2 Aug 2016 08:22:55 +0000 (UTC)
commit e761de9c2bee90c232875bbc41e6e73e1f63e145
Author: Sébastien Wilmet <swilmet gnome org>
Date: Tue Aug 2 07:54:59 2016 +0200
Use a DSO destructor
For the unit tests in testsuite/, the destructor is not called because
libgtksourceview-core.la is statically linked to the unit test programs.
A solution would be to have the private function
_gtksourceview_shutdown() that can be called in unit tests.
But the destructor is correctly called in e.g. tests/test-widget or
gedit, so it is already useful.
At least with gobject-list
https://blogs.gnome.org/danni/2011/02/17/ld_preload-gobject-lifetime-debugging-tool/
we can verify that all GtkSource GObjects are finalized. Unfortunately
the GtkSourceView destructor runs *after* the "Still Alive:" list that
gobject-list prints. But by counting the number of "Finalized object"
lines that follow, we can verify that all GObject are finalized.
gtksourceview/gtksourcelanguagemanager.c | 20 ++++++++++-----
gtksourceview/gtksourcelanguagemanager.h | 3 ++
gtksourceview/gtksourcestyleschememanager.c | 20 ++++++++++-----
gtksourceview/gtksourcestyleschememanager.h | 4 +++
gtksourceview/gtksourceview-init.c | 34 ++++++++++++++++++++++++++-
5 files changed, 66 insertions(+), 15 deletions(-)
---
diff --git a/gtksourceview/gtksourcelanguagemanager.c b/gtksourceview/gtksourcelanguagemanager.c
index ff3850d..3f56a35 100644
--- a/gtksourceview/gtksourcelanguagemanager.c
+++ b/gtksourceview/gtksourcelanguagemanager.c
@@ -68,6 +68,8 @@ struct _GtkSourceLanguageManagerPrivate
gchar **ids; /* Cache the IDs of the available languages */
};
+static GtkSourceLanguageManager *default_instance;
+
G_DEFINE_TYPE_WITH_PRIVATE (GtkSourceLanguageManager, gtk_source_language_manager, G_TYPE_OBJECT)
static void
@@ -203,16 +205,20 @@ gtk_source_language_manager_new (void)
GtkSourceLanguageManager *
gtk_source_language_manager_get_default (void)
{
- static GtkSourceLanguageManager *instance;
-
- if (instance == NULL)
+ if (default_instance == NULL)
{
- instance = gtk_source_language_manager_new ();
- g_object_add_weak_pointer (G_OBJECT (instance),
- (gpointer) &instance);
+ default_instance = gtk_source_language_manager_new ();
+ g_object_add_weak_pointer (G_OBJECT (default_instance),
+ (gpointer) &default_instance);
}
- return instance;
+ return default_instance;
+}
+
+GtkSourceLanguageManager *
+_gtk_source_language_manager_peek_default (void)
+{
+ return default_instance;
}
static void
diff --git a/gtksourceview/gtksourcelanguagemanager.h b/gtksourceview/gtksourcelanguagemanager.h
index d3d7657..71ce257 100644
--- a/gtksourceview/gtksourcelanguagemanager.h
+++ b/gtksourceview/gtksourcelanguagemanager.h
@@ -92,6 +92,9 @@ GtkSourceLanguage *gtk_source_language_manager_guess_language
(GtkSourceLangua
const gchar
*filename,
const gchar
*content_type);
+G_GNUC_INTERNAL
+GtkSourceLanguageManager *_gtk_source_language_manager_peek_default (void);
+
G_END_DECLS
#endif /* GTK_SOURCE_LANGUAGE_MANAGER_H */
diff --git a/gtksourceview/gtksourcestyleschememanager.c b/gtksourceview/gtksourcestyleschememanager.c
index 161536e..eeae1fd 100644
--- a/gtksourceview/gtksourcestyleschememanager.c
+++ b/gtksourceview/gtksourcestyleschememanager.c
@@ -57,6 +57,8 @@ enum {
PROP_SCHEME_IDS
};
+static GtkSourceStyleSchemeManager *default_instance;
+
G_DEFINE_TYPE_WITH_PRIVATE (GtkSourceStyleSchemeManager, gtk_source_style_scheme_manager, G_TYPE_OBJECT)
static void
@@ -202,16 +204,20 @@ gtk_source_style_scheme_manager_new (void)
GtkSourceStyleSchemeManager *
gtk_source_style_scheme_manager_get_default (void)
{
- static GtkSourceStyleSchemeManager *instance;
-
- if (instance == NULL)
+ if (default_instance == NULL)
{
- instance = gtk_source_style_scheme_manager_new ();
- g_object_add_weak_pointer (G_OBJECT (instance),
- (gpointer) &instance);
+ default_instance = gtk_source_style_scheme_manager_new ();
+ g_object_add_weak_pointer (G_OBJECT (default_instance),
+ (gpointer) &default_instance);
}
- return instance;
+ return default_instance;
+}
+
+GtkSourceStyleSchemeManager *
+_gtk_source_style_scheme_manager_peek_default (void)
+{
+ return default_instance;
}
static gboolean
diff --git a/gtksourceview/gtksourcestyleschememanager.h b/gtksourceview/gtksourcestyleschememanager.h
index ed35d0a..f0225ea 100644
--- a/gtksourceview/gtksourcestyleschememanager.h
+++ b/gtksourceview/gtksourcestyleschememanager.h
@@ -99,6 +99,10 @@ GTK_SOURCE_AVAILABLE_IN_ALL
GtkSourceStyleScheme *gtk_source_style_scheme_manager_get_scheme (GtkSourceStyleSchemeManager
*manager,
const gchar
*scheme_id);
+G_GNUC_INTERNAL
+GtkSourceStyleSchemeManager *
+ _gtk_source_style_scheme_manager_peek_default (void);
+
G_END_DECLS
#endif /* GTK_SOURCE_STYLE_SCHEME_MANAGER_H */
diff --git a/gtksourceview/gtksourceview-init.c b/gtksourceview/gtksourceview-init.c
index 03eb647..1403c7e 100644
--- a/gtksourceview/gtksourceview-init.c
+++ b/gtksourceview/gtksourceview-init.c
@@ -19,7 +19,10 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
-/* Init i18n */
+/* Constructor to init i18n.
+ * Destructor to release remaining allocated memory (useful when using
+ * memory-debugging tools).
+ */
#ifdef HAVE_CONFIG_H
#include <config.h>
@@ -30,6 +33,8 @@
#endif
#include "gconstructor.h"
+#include "gtksourcelanguagemanager.h"
+#include "gtksourcestyleschememanager.h"
#ifdef G_OS_WIN32
static HMODULE gtksourceview_dll;
@@ -127,6 +132,19 @@ gtksourceview_init (void)
#endif /* ENABLE_NLS */
}
+static void
+gtksourceview_shutdown (void)
+{
+ GtkSourceLanguageManager *language_manager;
+ GtkSourceStyleSchemeManager *style_scheme_manager;
+
+ language_manager = _gtk_source_language_manager_peek_default ();
+ g_clear_object (&language_manager);
+
+ style_scheme_manager = _gtk_source_style_scheme_manager_peek_default ();
+ g_clear_object (&style_scheme_manager);
+}
+
#if defined (G_OS_WIN32)
BOOL WINAPI DllMain (HINSTANCE hinstDLL,
@@ -146,6 +164,9 @@ DllMain (HINSTANCE hinstDLL,
break;
case DLL_THREAD_DETACH:
+ gtksourceview_shutdown ();
+ break;
+
default:
/* do nothing */
break;
@@ -167,6 +188,17 @@ gtksourceview_constructor (void)
gtksourceview_init ();
}
+# ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
+# pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(gtksourceview_destructor)
+# endif
+G_DEFINE_DESTRUCTOR (gtksourceview_destructor)
+
+static void
+gtksourceview_destructor (void)
+{
+ gtksourceview_shutdown ();
+}
+
#else
# error Your platform/compiler is missing constructor support
#endif
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]