[Spice-devel] [PATCH 3/9] RedsState: keep weak reference to channels

Jonathon Jongsma jjongsma at redhat.com
Fri Oct 28 17:06:23 UTC 2016


Keep a weak references to all registered channels, and free the list of
channels upon destruction.
---
 server/reds.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/server/reds.c b/server/reds.c
index c2ac4d9..9034197 100644
--- a/server/reds.c
+++ b/server/reds.c
@@ -452,9 +452,16 @@ void stat_remove_counter(RedsState *reds, uint64_t *counter)
 }
 #endif
 
+static void reds_channel_destroyed(gpointer data, GObject *ex_object)
+{
+    RedsState *reds = data;
+    reds->channels = g_list_remove(reds->channels, ex_object);
+}
+
 void reds_register_channel(RedsState *reds, RedChannel *channel)
 {
     spice_assert(reds);
+    g_object_weak_ref(G_OBJECT(channel), reds_channel_destroyed, reds);
     reds->channels = g_list_prepend(reds->channels, channel);
 }
 
@@ -3676,9 +3683,16 @@ SPICE_GNUC_VISIBLE int spice_server_init(SpiceServer *reds, SpiceCoreInterface *
 
 SPICE_GNUC_VISIBLE void spice_server_destroy(SpiceServer *reds)
 {
+    GList *l;
+
     g_array_unref(reds->config->renderers);
     g_array_unref(reds->config->video_codecs);
     free(reds->config);
+
+    for (l = reds->channels; l != NULL; l = l->next)
+        g_object_weak_unref(l->data, reds_channel_destroyed, reds);
+    g_list_free(reds->channels);
+
     if (reds->main_channel) {
         main_channel_close(reds->main_channel);
         g_object_unref(reds->main_channel);
-- 
2.7.4



More information about the Spice-devel mailing list