[spice-server,4/4] red-client: Make sure MainChannelClient is freed as last

Submitted by Frediano Ziglio on Aug. 30, 2017, 12:51 p.m.

Details

Message ID 20170830125128.9453-4-fziglio@redhat.com
State New
Headers show
Series "Series without cover letter" ( rev: 1 ) in Spice

Not browsing as part of any series.

Commit Message

Frediano Ziglio Aug. 30, 2017, 12:51 p.m.
MainChannelClient is used by other clients to store some data
so should not disappear if other clients are still present.
Keep a owning reference to it and release after RedClient is
released.

Signed-off-by: Frediano Ziglio <fziglio@redhat.com>
---
 server/red-client.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

Patch hide | download patch | download mbox

diff --git a/server/red-client.c b/server/red-client.c
index 36577dab..7e1394e5 100644
--- a/server/red-client.c
+++ b/server/red-client.c
@@ -108,6 +108,10 @@  red_client_finalize (GObject *object)
 {
     RedClient *self = RED_CLIENT(object);
 
+    if (self->mcc) {
+        g_object_unref(self->mcc);
+        self->mcc = NULL;
+    }
     spice_debug("release client=%p", self);
     pthread_mutex_destroy(&self->lock);
 
@@ -314,7 +318,8 @@  MainChannelClient *red_client_get_main(RedClient *client)
 
 void red_client_set_main(RedClient *client, MainChannelClient *mcc)
 {
-    client->mcc = mcc;
+    spice_assert(client->mcc == NULL);
+    client->mcc = g_object_ref(mcc);
 }
 
 void red_client_semi_seamless_migrate_complete(RedClient *client)

Comments

On Wed, Aug 30, 2017 at 01:51:28PM +0100, Frediano Ziglio wrote:
> MainChannelClient is used by other clients to store some data
> so should not disappear if other clients are still present.
> Keep a owning reference to it and release after RedClient is
> released.
> 
> Signed-off-by: Frediano Ziglio <fziglio@redhat.com>
> ---
>  server/red-client.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/server/red-client.c b/server/red-client.c
> index 36577dab..7e1394e5 100644
> --- a/server/red-client.c
> +++ b/server/red-client.c
> @@ -108,6 +108,10 @@ red_client_finalize (GObject *object)
>  {
>      RedClient *self = RED_CLIENT(object);
>  
> +    if (self->mcc) {
> +        g_object_unref(self->mcc);
> +        self->mcc = NULL;
> +    }

This could be g_clear_object(&self->mcc);

>      spice_debug("release client=%p", self);
>      pthread_mutex_destroy(&self->lock);
>  
> @@ -314,7 +318,8 @@ MainChannelClient *red_client_get_main(RedClient *client)
>  
>  void red_client_set_main(RedClient *client, MainChannelClient *mcc)
>  {
> -    client->mcc = mcc;
> +    spice_assert(client->mcc == NULL);

Why not
if (client->mcc != NULL) {
    g_warn_if_reached();
    g_object_unref(client->mcc);
}
client->mcc = g_object_ref(mcc);
?

Acked-by: Christophe Fergeau <cfergeau@redhat.com>