[libvirt] [libvirt-glib v2] gobject: Fix GMutex leak

Christophe Fergeau cfergeau at redhat.com
Wed Nov 14 14:16:57 UTC 2012


When GLib deprecated g_mutex_new/g_mutex_free, we introduced a
compatibility wrapper to implement these using non-deprecated
functions. This was done by allocating 0-filled memory by the
mutex, and then letting GLib initialize the structure when
needed. However, we must call g_mutex_clear when destroying
the mutex to free these resources, which this patch fix.
---
 libvirt-gobject/libvirt-gobject-compat.c | 16 ++++++++++++++++
 libvirt-gobject/libvirt-gobject-compat.h |  8 ++++++--
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/libvirt-gobject/libvirt-gobject-compat.c b/libvirt-gobject/libvirt-gobject-compat.c
index 99cd44c..14b5eb3 100644
--- a/libvirt-gobject/libvirt-gobject-compat.c
+++ b/libvirt-gobject/libvirt-gobject-compat.c
@@ -103,3 +103,19 @@ g_simple_async_report_take_gerror_in_idle(GObject *object,
     g_object_unref(simple);
 }
 #endif
+
+GMutex *gvir_mutex_new(void)
+{
+    GMutex *mutex;
+
+    mutex = g_new(GMutex, 1);
+    g_mutex_init(mutex);
+
+    return mutex;
+}
+
+void gvir_mutex_free(GMutex *mutex)
+{
+    g_mutex_clear(mutex);
+    g_free(mutex);
+}
diff --git a/libvirt-gobject/libvirt-gobject-compat.h b/libvirt-gobject/libvirt-gobject-compat.h
index 839dfe1..1d45018 100644
--- a/libvirt-gobject/libvirt-gobject-compat.h
+++ b/libvirt-gobject/libvirt-gobject-compat.h
@@ -27,8 +27,12 @@
 #include <gio/gio.h>
 
 #if GLIB_CHECK_VERSION(2, 31, 0)
-#define g_mutex_new() g_new0(GMutex, 1)
-#define g_mutex_free(m) g_free(m)
+
+void gvir_mutex_free(GMutex *mutex);
+GMutex *gvir_mutex_new(void);
+#define g_mutex_new gvir_mutex_new
+#define g_mutex_free gvir_mutex_free
+
 #endif
 
 #if !GLIB_CHECK_VERSION(2,26,0)
-- 
1.8.0




More information about the libvir-list mailing list