[libvirt] [libvirt-glib 4/5] gobject: Fix GMutex leak

Christophe Fergeau cfergeau at redhat.com
Tue Nov 13 17:30:30 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.h | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/libvirt-gobject/libvirt-gobject-compat.h b/libvirt-gobject/libvirt-gobject-compat.h
index 839dfe1..40e118d 100644
--- a/libvirt-gobject/libvirt-gobject-compat.h
+++ b/libvirt-gobject/libvirt-gobject-compat.h
@@ -27,8 +27,24 @@
 #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)
+static inline GMutex *gvir_mutex_new(void)
+{
+    GMutex *mutex;
+
+    mutex = g_new(GMutex, 1);
+    g_mutex_init(mutex);
+
+    return mutex;
+}
+
+static inline void gvir_mutex_free(GMutex *mutex)
+{
+    g_mutex_clear(mutex);
+    g_free(mutex);
+}
+
+#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