[virt-tools-list] [libosinfo 2/8] Make OsinfoMedia::os a weak reference

Christophe Fergeau cfergeau at redhat.com
Mon Dec 3 11:23:31 UTC 2012


OsinfoOs stores a list of media when it's part of an OsinfoDb. If
these OsinfoMedia took a reference on the OsinfoOs they are part
of, we'd get a circular reference.
---
 osinfo/osinfo_media.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/osinfo/osinfo_media.c b/osinfo/osinfo_media.c
index 6d0bdec..c25ca5a 100644
--- a/osinfo/osinfo_media.c
+++ b/osinfo/osinfo_media.c
@@ -136,7 +136,7 @@ G_DEFINE_TYPE (OsinfoMedia, osinfo_media, OSINFO_TYPE_ENTITY);
 
 struct _OsinfoMediaPrivate
 {
-    OsinfoOs *os;
+    GWeakRef os;
 };
 
 enum {
@@ -221,7 +221,7 @@ osinfo_media_get_property (GObject    *object,
         break;
 
     case PROP_OS:
-        g_value_set_object (value, osinfo_media_get_os (media));
+        g_value_take_object (value, osinfo_media_get_os (media));
         break;
 
     default:
@@ -328,7 +328,7 @@ static void osinfo_media_dispose(GObject *obj)
 {
     OsinfoMedia *media = OSINFO_MEDIA(obj);
 
-    g_clear_object(&media->priv->os);
+    g_weak_ref_clear(&media->priv->os);
 
     G_OBJECT_CLASS(osinfo_media_parent_class)->dispose(obj);
 }
@@ -521,6 +521,7 @@ osinfo_media_init (OsinfoMedia *media)
 {
     OsinfoMediaPrivate *priv;
     media->priv = priv = OSINFO_MEDIA_GET_PRIVATE(media);
+    g_weak_ref_init(&media->priv->os, NULL);
 }
 
 OsinfoMedia *osinfo_media_new(const gchar *id,
@@ -1080,20 +1081,23 @@ gint osinfo_media_get_installer_reboots(OsinfoMedia *media)
             (OSINFO_ENTITY(media), OSINFO_MEDIA_PROP_INSTALLER_REBOOTS, 1);
 }
 
+/* osinfo_media_get_os:
+ * Returns: (transfer full):
+ */
 OsinfoOs *osinfo_media_get_os(OsinfoMedia *media)
 {
     g_return_val_if_fail(OSINFO_IS_MEDIA(media), NULL);
 
-    return media->priv->os;
+    return g_weak_ref_get(&media->priv->os);
 }
 
 void osinfo_media_set_os(OsinfoMedia *media, OsinfoOs *os)
 {
     g_return_if_fail(OSINFO_IS_MEDIA(media));
 
-    if (media->priv->os != NULL)
-        g_object_unref(media->priv->os);
-    media->priv->os = g_object_ref(os);
+    g_object_ref(os);
+    g_weak_ref_set(&media->priv->os, os);
+    g_object_unref(os);
 }
 /*
  * Local variables:
-- 
1.8.0.1




More information about the virt-tools-list mailing list