diff --git a/server/display-channel.c b/server/display-channel.c index 52f0a3dd..061a99d0 100644 --- a/server/display-channel.c +++ b/server/display-channel.c @@ -21,6 +21,7 @@ #include #include "display-channel-private.h" +#include "glib-compat.h" G_DEFINE_TYPE(DisplayChannel, display_channel, TYPE_COMMON_GRAPHICS_CHANNEL) @@ -62,10 +63,7 @@ display_channel_set_property(GObject *object, self->priv->n_surfaces = g_value_get_uint(value); break; case PROP_VIDEO_CODECS: - if (self->priv->video_codecs) { - g_array_unref(self->priv->video_codecs); - } - self->priv->video_codecs = g_array_ref(g_value_get_boxed(value)); + display_channel_set_video_codecs(self, g_value_get_boxed(value)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec); @@ -211,7 +209,7 @@ void display_channel_set_video_codecs(DisplayChannel *display, GArray *video_cod { spice_return_if_fail(display); - g_array_unref(display->priv->video_codecs); + g_clear_pointer(&display->priv->video_codecs, g_array_unref); display->priv->video_codecs = g_array_ref(video_codecs); } diff --git a/server/glib-compat.h b/server/glib-compat.h index 8a27afe5..50a07839 100644 --- a/server/glib-compat.h +++ b/server/glib-compat.h @@ -20,6 +20,25 @@ #include +#if !GLIB_CHECK_VERSION(2,34,0) +#define g_clear_pointer(pp, destroy) \ + G_STMT_START { \ + G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer)); \ + /* Only one access, please */ \ + gpointer *_pp = (gpointer *) (pp); \ + gpointer _p; \ + /* This assignment is needed to avoid a gcc warning */ \ + GDestroyNotify _destroy = (GDestroyNotify) (destroy); \ + \ + _p = *_pp; \ + if (_p) \ + { \ + *_pp = NULL; \ + _destroy (_p); \ + } \ + } G_STMT_END +#endif + #if !GLIB_CHECK_VERSION(2,32,0) static inline void g_queue_free_full(GQueue *queue, GDestroyNotify free_func)