[Spice-devel] [PATCH spice-gtk 4/4] Use g_clear_pointer if possible

Eduardo Lima (Etrunko) etrunko at redhat.com
Tue Apr 5 17:43:33 UTC 2016


On 04/05/2016 01:58 PM, Pavel Grunt wrote:
> ---
>  src/bio-gio.c               |  3 +--
>  src/channel-display-mjpeg.c |  6 ++----
>  src/channel-display.c       | 10 +++-------
>  src/channel-main.c          |  6 ++----
>  src/channel-playback.c      |  3 +--
>  src/channel-record.c        |  9 +++------
>  src/channel-smartcard.c     | 28 ++++++---------------------
>  src/channel-usbredir.c      |  9 +++------
>  src/decode-glz.c            |  3 +--
>  src/smartcard-manager.c     | 13 +++----------
>  src/spice-audio.c           |  5 +----
>  src/spice-channel.c         | 26 ++++++-------------------
>  src/spice-gstaudio.c        | 30 +++++++++--------------------
>  src/spice-gtk-session.c     |  6 ++----
>  src/spice-option.c          |  3 +--
>  src/spice-pulse.c           | 46 +++++++++++++--------------------------------
>  src/spice-session.c         | 12 ++++--------
>  src/spice-widget-cairo.c    | 10 +++-------
>  src/spice-widget.c          | 16 ++++------------
>  src/usb-acl-helper.c        | 11 ++---------
>  src/wocky-http-proxy.c      |  3 +--
>  21 files changed, 71 insertions(+), 187 deletions(-)
> 

Wow, that's quite a lot of files changed. I wonder if those variables
set to NULL are really reused that often to justify cleaning up like
that. I found a couple of mistakes and also commented about getting rid
of some curly braces.

Reviewed-by: Eduardo Lima (Etrunko) <etrunko at redhat.com>


> diff --git a/src/bio-gio.c b/src/bio-gio.c
> index 108ac1a..b310c97 100644
> --- a/src/bio-gio.c
> +++ b/src/bio-gio.c
> @@ -76,8 +76,7 @@ static int bio_gio_destroy(BIO *bio)
>          return 0;
>  
>      SPICE_DEBUG("bio gsocket destroy");
> -    g_free(bio->method);
> -    bio->method = NULL;;
> +    g_clear_pointer(&bio->method, g_free);
>  
>      return 1;
>  }
> diff --git a/src/channel-display-mjpeg.c b/src/channel-display-mjpeg.c
> index 95d5b33..25b6bdd 100644
> --- a/src/channel-display-mjpeg.c
> +++ b/src/channel-display-mjpeg.c
> @@ -75,8 +75,7 @@ void stream_mjpeg_data(display_stream *st)
>      stream_get_dimensions(st, &width, &height);
>      dest = g_malloc0(width * height * 4);
>  
> -    g_free(st->out_frame);
> -    st->out_frame = dest;
> +    g_clear_pointer(&st->out_frame, g_free);

Original code sets out_frame to dest, not NULL.

>  
>      jpeg_read_header(&st->mjpeg_cinfo, 1);
>  #ifdef JCS_EXTENSIONS
> @@ -151,6 +150,5 @@ G_GNUC_INTERNAL
>  void stream_mjpeg_cleanup(display_stream *st)
>  {
>      jpeg_destroy_decompress(&st->mjpeg_cinfo);
> -    g_free(st->out_frame);
> -    st->out_frame = NULL;
> +    g_clear_pointer(&st->out_frame, g_free);
>  }
> diff --git a/src/channel-display.c b/src/channel-display.c
> index 431c2e7..b6c79e0 100644
> --- a/src/channel-display.c
> +++ b/src/channel-display.c
> @@ -831,11 +831,8 @@ static void destroy_canvas(display_surface *surface)
>      zlib_decoder_destroy(surface->zlib_decoder);
>      jpeg_decoder_destroy(surface->jpeg_decoder);
>  
> -    g_free(surface->data);
> -    surface->data = NULL;
> -
> -    surface->canvas->ops->destroy(surface->canvas);
> -    surface->canvas = NULL;
> +    g_clear_pointer(&surface->data, g_free);
> +    g_clear_pointer(&surface->canvas, surface->canvas->ops->destroy);
>  }
>  
>  static display_surface *find_surface(SpiceDisplayChannelPrivate *c, guint32 surface_id)
> @@ -1597,8 +1594,7 @@ static void clear_streams(SpiceChannel *channel)
>      for (i = 0; i < c->nstreams; i++) {
>          destroy_stream(channel, i);
>      }
> -    g_free(c->streams);
> -    c->streams = NULL;
> +    g_clear_pointer(&c->streams, g_free);
>      c->nstreams = 0;
>  }
>  
> diff --git a/src/channel-main.c b/src/channel-main.c
> index 4a1f58a..955b358 100644
> --- a/src/channel-main.c
> +++ b/src/channel-main.c
> @@ -449,8 +449,7 @@ static void spice_main_channel_reset_agent(SpiceMainChannel *channel)
>      c->agent_caps_received = FALSE;
>      c->agent_display_config_sent = FALSE;
>      c->agent_msg_pos = 0;
> -    g_free(c->agent_msg_data);
> -    c->agent_msg_data = NULL;
> +    g_clear_pointer(&c->agent_msg_data, g_free);
>      c->agent_msg_size = 0;
>  
>      tasks = g_hash_table_get_values(c->file_xfer_tasks);
> @@ -916,8 +915,7 @@ static void agent_free_msg_queue(SpiceMainChannel *channel)
>          spice_msg_out_unref(out);
>      }
>  
> -    g_queue_free(c->agent_msg_queue);
> -    c->agent_msg_queue = NULL;
> +    g_clear_pointer(&c->agent_msg_queue, g_queue_free);
>  }
>  
>  static gboolean flush_foreach_remove(gpointer key G_GNUC_UNUSED,
> diff --git a/src/channel-playback.c b/src/channel-playback.c
> index c97afd8..e931727 100644
> --- a/src/channel-playback.c
> +++ b/src/channel-playback.c
> @@ -114,8 +114,7 @@ static void spice_playback_channel_finalize(GObject *obj)
>  
>      snd_codec_destroy(&c->codec);
>  
> -    g_free(c->volume);
> -    c->volume = NULL;
> +    g_clear_pointer(&c->volume, g_free);
>  
>      if (G_OBJECT_CLASS(spice_playback_channel_parent_class)->finalize)
>          G_OBJECT_CLASS(spice_playback_channel_parent_class)->finalize(obj);
> diff --git a/src/channel-record.c b/src/channel-record.c
> index 08269c9..e48c4b4 100644
> --- a/src/channel-record.c
> +++ b/src/channel-record.c
> @@ -108,13 +108,11 @@ static void spice_record_channel_finalize(GObject *obj)
>  {
>      SpiceRecordChannelPrivate *c = SPICE_RECORD_CHANNEL(obj)->priv;
>  
> -    g_free(c->last_frame);
> -    c->last_frame = NULL;
> +    g_clear_pointer(&c->last_frame, g_free);
>  
>      snd_codec_destroy(&c->codec);
>  
> -    g_free(c->volume);
> -    c->volume = NULL;
> +    g_clear_pointer(&c->volume, g_free);
>  
>      if (G_OBJECT_CLASS(spice_record_channel_parent_class)->finalize)
>          G_OBJECT_CLASS(spice_record_channel_parent_class)->finalize(obj);
> @@ -166,8 +164,7 @@ static void channel_reset(SpiceChannel *channel, gboolean migrating)
>  {
>      SpiceRecordChannelPrivate *c = SPICE_RECORD_CHANNEL(channel)->priv;
>  
> -    g_free(c->last_frame);
> -    c->last_frame = NULL;
> +    g_clear_pointer(&c->last_frame, NULL);
>  

+    g_clear_pointer(&c->last_frame, g_free);


>      g_coroutine_signal_emit(channel, signals[SPICE_RECORD_STOP], 0);
>      c->started = FALSE;
> diff --git a/src/channel-smartcard.c b/src/channel-smartcard.c
> index 21df98a..09e784d 100644
> --- a/src/channel-smartcard.c
> +++ b/src/channel-smartcard.c
> @@ -161,26 +161,15 @@ static void spice_smartcard_channel_finalize(GObject *obj)
>      SpiceSmartcardChannel *channel = SPICE_SMARTCARD_CHANNEL(obj);
>      SpiceSmartcardChannelPrivate *c = channel->priv;
>  
> -    if (c->pending_card_insertions != NULL) {
> -        g_hash_table_destroy(c->pending_card_insertions);
> -        c->pending_card_insertions = NULL;
> -    }
> -    if (c->pending_reader_removals != NULL) {
> -        g_hash_table_destroy(c->pending_reader_removals);
> -        c->pending_reader_removals = NULL;
> -    }
> +    g_clear_pointer(&c->pending_card_insertions, g_hash_table_destroy);
> +    g_clear_pointer(&c->pending_reader_removals, g_hash_table_destroy);
>      if (c->message_queue != NULL) {
>          g_queue_foreach(c->message_queue, (GFunc)smartcard_message_free, NULL);
>          g_queue_free(c->message_queue);
>          c->message_queue = NULL;
>      }
> -    if (c->in_flight_message != NULL) {
> -        smartcard_message_free(c->in_flight_message);
> -        c->in_flight_message = NULL;
> -    }
> -
> -    g_list_free(c->pending_reader_additions);
> -    c->pending_reader_additions = NULL;
> +    g_clear_pointer(&c->in_flight_message, smartcard_message_free);
> +    g_clear_pointer(&c->pending_reader_additions, g_list_free);
>  
>      if (G_OBJECT_CLASS(spice_smartcard_channel_parent_class)->finalize)
>          G_OBJECT_CLASS(spice_smartcard_channel_parent_class)->finalize(obj);
> @@ -199,13 +188,8 @@ static void spice_smartcard_channel_reset(SpiceChannel *channel, gboolean migrat
>          g_queue_clear(c->message_queue);
>      }
>  
> -    if (c->in_flight_message != NULL) {
> -        smartcard_message_free(c->in_flight_message);
> -        c->in_flight_message = NULL;
> -    }
> -
> -    g_list_free(c->pending_reader_additions);
> -    c->pending_reader_additions = NULL;
> +    g_clear_pointer(&c->in_flight_message, smartcard_message_free);
> +    g_clear_pointer(&c->pending_reader_additions, g_list_free);
>  
>      SPICE_CHANNEL_CLASS(spice_smartcard_channel_parent_class)->channel_reset(channel, migrating);
>  }
> diff --git a/src/channel-usbredir.c b/src/channel-usbredir.c
> index 9066b4a..c8a2da9 100644
> --- a/src/channel-usbredir.c
> +++ b/src/channel-usbredir.c
> @@ -335,8 +335,7 @@ static void spice_usbredir_channel_open_acl_cb(
>          spice_usbredir_channel_open_device(channel, &err);
>      }
>      if (err) {
> -        libusb_unref_device(priv->device);
> -        priv->device = NULL;
> +        g_clear_pointer(&priv->device, libusb_unref_device);
>          g_boxed_free(spice_usb_device_get_type(), priv->spice_device);
>          priv->spice_device = NULL;
>          priv->state  = STATE_DISCONNECTED;
> @@ -367,8 +366,7 @@ _open_device_async_cb(GTask *task,
>      spice_usbredir_channel_lock(channel);
>  
>      if (!spice_usbredir_channel_open_device(channel, &err)) {
> -        libusb_unref_device(priv->device);
> -        priv->device = NULL;
> +        g_clear_pointer(&priv->device, libusb_unref_device);
>          g_boxed_free(spice_usb_device_get_type(), priv->spice_device);
>          priv->spice_device = NULL;
>      }
> @@ -491,8 +489,7 @@ void spice_usbredir_channel_disconnect_device(SpiceUsbredirChannel *channel)
>          }
>          /* This also closes the libusb handle we passed from open_device */
>          usbredirhost_set_device(priv->host, NULL);
> -        libusb_unref_device(priv->device);
> -        priv->device = NULL;
> +        g_clear_pointer(&priv->device, libusb_unref_device);
>          g_boxed_free(spice_usb_device_get_type(), priv->spice_device);
>          priv->spice_device = NULL;
>          priv->state  = STATE_DISCONNECTED;
> diff --git a/src/decode-glz.c b/src/decode-glz.c
> index a5fb6c1..b7dd2e6 100644
> --- a/src/decode-glz.c
> +++ b/src/decode-glz.c
> @@ -173,8 +173,7 @@ static void glz_decoder_window_release(SpiceGlzDecoderWindow *w,
>  
>      while (w->oldest < oldest) {
>          slot = w->oldest % w->nimages;
> -        glz_image_destroy(w->images[slot]);
> -        w->images[slot] = NULL;
> +        g_clear_pointer(&w->images[slot], glz_image_destroy);
>          w->oldest++;
>      }
>  }
> diff --git a/src/smartcard-manager.c b/src/smartcard-manager.c
> index d3a40ba..708f976 100644
> --- a/src/smartcard-manager.c
> +++ b/src/smartcard-manager.c
> @@ -127,10 +127,7 @@ static void spice_smartcard_manager_finalize(GObject *gobject)
>      }
>  
>  #ifdef USE_SMARTCARD
> -    if (priv->software_reader != NULL) {
> -        vreader_free(priv->software_reader);
> -        priv->software_reader = NULL;
> -    }
> +    g_clear_pointer(&priv->software_reader, vreader_free);
>  #endif
>  
>      /* Chain up to the parent class */
> @@ -275,8 +272,7 @@ static gboolean smartcard_monitor_dispatch(VEvent *event, gpointer user_data)
>          case VEVENT_READER_REMOVE:
>              if (spice_smartcard_reader_is_software((SpiceSmartcardReader*)event->reader)) {
>                  g_warn_if_fail(manager->priv->software_reader != NULL);
> -                vreader_free(manager->priv->software_reader);
> -                manager->priv->software_reader = NULL;
> +                g_clear_pointer(&manager->priv->software_reader, vreader_free);
>              }
>              SPICE_DEBUG("smartcard: reader-removed");
>              g_signal_emit(G_OBJECT(user_data),
> @@ -355,10 +351,7 @@ static void smartcard_source_finalize(GSource *source)
>  {
>      SmartcardSource *smartcard_source = (SmartcardSource *)source;
>  
> -    if (smartcard_source->pending_event) {
> -        vevent_delete(smartcard_source->pending_event);
> -        smartcard_source->pending_event = NULL;
> -    }
> +    g_clear_pointer(&smartcard_source->pending_event, vevent_delete);
>  }
>  
>  static GSource *smartcard_monitor_source_new(void)
> diff --git a/src/spice-audio.c b/src/spice-audio.c
> index 86a5138..c514d30 100644
> --- a/src/spice-audio.c
> +++ b/src/spice-audio.c
> @@ -65,10 +65,7 @@ static void spice_audio_finalize(GObject *gobject)
>      SpiceAudio *self = SPICE_AUDIO(gobject);
>      SpiceAudioPrivate *priv = self->priv;
>  
> -    if (priv->main_context) {
> -        g_main_context_unref(priv->main_context);
> -        priv->main_context = NULL;
> -    }
> +    g_clear_pointer(&priv->main_context, g_main_context_unref);
>  
>      if (G_OBJECT_CLASS(spice_audio_parent_class)->finalize)
>          G_OBJECT_CLASS(spice_audio_parent_class)->finalize(gobject);
> diff --git a/src/spice-channel.c b/src/spice-channel.c
> index e9c5a1b..19237b3 100644
> --- a/src/spice-channel.c
> +++ b/src/spice-channel.c
> @@ -1684,10 +1684,7 @@ restart:
>              goto restep;
>          }
>  
> -        if (serverin) {
> -            g_free(serverin);
> -            serverin = NULL;
> -        }
> +        g_clear_pointer(&serverin, g_free);
>  
>          CHANNEL_DEBUG(channel, "Client step result %d. Data %d bytes %p '%s'", err, clientoutlen, clientout, clientout);
>  
> @@ -1739,8 +1736,7 @@ restart:
>  
>          /* This server call shows complete, and earlier client step was OK */
>          if (complete) {
> -            g_free(serverin);
> -            serverin = NULL;
> +            g_clear_pointer(&serverin, g_free);
>              if (err == SASL_CONTINUE) /* something went wrong */
>                  goto complete;
>              break;
> @@ -2702,18 +2698,9 @@ static void channel_reset(SpiceChannel *channel, gboolean migrating)
>      }
>  #endif
>  
> -    spice_openssl_verify_free(c->sslverify);
> -    c->sslverify = NULL;
> -
> -    if (c->ssl) {
> -        SSL_free(c->ssl);
> -        c->ssl = NULL;
> -    }
> -
> -    if (c->ctx) {
> -        SSL_CTX_free(c->ctx);
> -        c->ctx = NULL;
> -    }
> +    g_clear_pointer(&c->sslverify, spice_openssl_verify_free);
> +    g_clear_pointer(&c->ssl, SSL_free);
> +    g_clear_pointer(&c->ctx, SSL_CTX_free);
>  
>      g_clear_object(&c->conn);
>      g_clear_object(&c->sock);
> @@ -2723,8 +2710,7 @@ static void channel_reset(SpiceChannel *channel, gboolean migrating)
>      c->auth_needs_username = FALSE;
>      c->auth_needs_password = FALSE;
>  
> -    g_free(c->peer_msg);
> -    c->peer_msg = NULL;
> +    g_clear_pointer(&c->peer_msg, g_free);
>      c->peer_pos = 0;
>  
>      STATIC_MUTEX_LOCK(c->xmit_queue_lock);
> diff --git a/src/spice-gstaudio.c b/src/spice-gstaudio.c
> index a7c3c24..fa0a074 100644
> --- a/src/spice-gstaudio.c
> +++ b/src/spice-gstaudio.c
> @@ -70,19 +70,11 @@ void stream_dispose(struct stream *s)
>  {
>      if (s->pipe) {
>          gst_element_set_state(s->pipe, GST_STATE_NULL);
> -        gst_object_unref(s->pipe);
> -        s->pipe = NULL;
> +        g_clear_pointer(&s->pipe, gst_object_unref);
>      }
>  
> -    if (s->src) {
> -        gst_object_unref(s->src);
> -        s->src = NULL;
> -    }
> -
> -    if (s->sink) {
> -        gst_object_unref(s->sink);
> -        s->sink = NULL;
> -    }
> +    g_clear_pointer(&s->src, gst_object_unref);
> +    g_clear_pointer(&s->sink, gst_object_unref);
>  }
>  
>  static void spice_gstaudio_dispose(GObject *obj)
> @@ -210,8 +202,7 @@ static void record_start(SpiceRecordChannel *channel, gint format, gint channels
>          (p->record.rate != frequency ||
>           p->record.channels != channels)) {
>          record_stop(gstaudio);
> -        gst_object_unref(p->record.pipe);
> -        p->record.pipe = NULL;
> +        g_clear_pointer(&p->record.pipe, gst_object_unref);
>      }
>  
>      if (!p->record.pipe) {
> @@ -244,9 +235,8 @@ static void record_start(SpiceRecordChannel *channel, gint format, gint channels
>                                        G_CALLBACK(record_new_buffer), gstaudio, 0);
>  
>  cleanup:
> -        if (error != NULL && p->record.pipe != NULL) {
> -            gst_object_unref(p->record.pipe);
> -            p->record.pipe = NULL;
> +        if (error != NULL) {
> +            g_clear_pointer(&p->record.pipe, gst_object_unref);
>          }

I would also get rid of curly braces here and below.


>          g_clear_error(&error);
>          g_free(audio_caps);
> @@ -303,8 +293,7 @@ static void playback_start(SpicePlaybackChannel *channel, gint format, gint chan
>          (p->playback.rate != frequency ||
>           p->playback.channels != channels)) {
>          playback_stop(gstaudio);
> -        gst_object_unref(p->playback.pipe);
> -        p->playback.pipe = NULL;
> +        g_clear_pointer(&p->playback.pipe, gst_object_unref);
>      }
>  
>      if (!p->playback.pipe) {
> @@ -328,9 +317,8 @@ static void playback_start(SpicePlaybackChannel *channel, gint format, gint chan
>          p->playback.channels = channels;
>  
>  cleanup:
> -        if (error != NULL && p->playback.pipe != NULL) {
> -            gst_object_unref(p->playback.pipe);
> -            p->playback.pipe = NULL;
> +        if (error != NULL) {
> +            g_clear_pointer(&p->playback.pipe, gst_object_unref);
>          }

Same as above.

>          g_clear_error(&error);
>          g_free(audio_caps);
> diff --git a/src/spice-gtk-session.c b/src/spice-gtk-session.c
> index 7370599..27623f0 100644
> --- a/src/spice-gtk-session.c
> +++ b/src/spice-gtk-session.c
> @@ -303,8 +303,7 @@ static void spice_gtk_session_finalize(GObject *gobject)
>  
>      /* release stuff */
>      for (i = 0; i < CLIPBOARD_LAST; ++i) {
> -        g_free(s->clip_targets[i]);
> -        s->clip_targets[i] = NULL;
> +        g_clear_pointer(&s->clip_targets[i], g_free);
>      }
>  
>      /* Chain up to the parent class */
> @@ -792,8 +791,7 @@ static void clipboard_get(GtkClipboard *clipboard,
>      gdk_threads_enter();
>  
>  cleanup:
> -    g_main_loop_unref(ri.loop);
> -    ri.loop = NULL;
> +    g_clear_pointer(&ri.loop, g_main_loop_unref);
>      g_signal_handler_disconnect(s->main, clipboard_handler);
>      g_signal_handler_disconnect(s->main, agent_handler);
>  }
> diff --git a/src/spice-option.c b/src/spice-option.c
> index 70d0277..46ae530 100644
> --- a/src/spice-option.c
> +++ b/src/spice-option.c
> @@ -99,8 +99,7 @@ static gboolean parse_disable_effects(const gchar *option_name, const gchar *val
>               */
>              g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
>                      _("invalid effect name (%s), must be 'wallpaper', 'font-smooth', 'animation' or 'all'"), *it);
> -            g_strfreev(disable_effects);
> -            disable_effects = NULL;
> +            g_clear_pointer(&disable_effects, g_strfreev);
>              return FALSE;
>          }
>      }
> diff --git a/src/spice-pulse.c b/src/spice-pulse.c
> index 6bc3014..fd5fe91 100644
> --- a/src/spice-pulse.c
> +++ b/src/spice-pulse.c
> @@ -131,21 +131,10 @@ static void spice_pulse_dispose(GObject *obj)
>      SPICE_DEBUG("%s", __FUNCTION__);
>      p = pulse->priv;
>  
> -    if (p->playback.uncork_op)
> -        pa_operation_unref(p->playback.uncork_op);
> -    p->playback.uncork_op = NULL;
> -
> -    if (p->playback.cork_op)
> -        pa_operation_unref(p->playback.cork_op);
> -    p->playback.cork_op = NULL;
> -
> -    if (p->record.uncork_op)
> -        pa_operation_unref(p->record.uncork_op);
> -    p->record.uncork_op = NULL;
> -
> -    if (p->record.cork_op)
> -        pa_operation_unref(p->record.cork_op);
> -    p->record.cork_op = NULL;
> +    g_clear_pointer(&p->playback.uncork_op, pa_operation_unref);
> +    g_clear_pointer(&p->playback.cork_op, pa_operation_unref);
> +    g_clear_pointer(&p->record.uncork_op, pa_operation_unref);
> +    g_clear_pointer(&p->record.cork_op, pa_operation_unref);
>  
>      if (p->results != NULL)
>          spice_pulse_complete_all_async_tasks(pulse, "PulseAudio is being dispose");
> @@ -194,8 +183,7 @@ static void pulse_uncork_cb(pa_stream *pastream, int success, void *data)
>      if (!success)
>          g_warning("pulseaudio uncork operation failed");
>  
> -    pa_operation_unref(s->uncork_op);
> -    s->uncork_op = NULL;
> +    g_clear_pointer(&s->uncork_op, pa_operation_unref);
>  }
>  
>  static void stream_uncork(SpicePulse *pulse, struct stream *s)
> @@ -207,8 +195,7 @@ static void stream_uncork(SpicePulse *pulse, struct stream *s)
>  
>      if (s->cork_op) {
>          pa_operation_cancel(s->cork_op);
> -        pa_operation_unref(s->cork_op);
> -        s->cork_op = NULL;
> +        g_clear_pointer(&s->cork_op, pa_operation_unref);
>      }
>  
>      if (pa_stream_is_corked(s->stream) && !s->uncork_op) {
> @@ -227,8 +214,7 @@ static void pulse_flush_cb(pa_stream *pastream, int success, void *data)
>      if (!success)
>          g_warning("pulseaudio flush operation failed");
>  
> -    pa_operation_unref(s->cork_op);
> -    s->cork_op = NULL;
> +    g_clear_pointer(&s->cork_op, pa_operation_unref);
>  }
>  
>  static void pulse_cork_flush_cb(pa_stream *pastream, int success, void *data)
> @@ -253,8 +239,7 @@ static void pulse_cork_cb(pa_stream *pastream, int success, void *data)
>      if (!success)
>          g_warning("pulseaudio cork operation failed");
>  
> -    pa_operation_unref(s->cork_op);
> -    s->cork_op = NULL;
> +    g_clear_pointer(&s->cork_op, pa_operation_unref);
>  }
>  
>  static void stream_cork(SpicePulse *pulse, struct stream *s, gboolean with_flush)
> @@ -264,8 +249,7 @@ static void stream_cork(SpicePulse *pulse, struct stream *s, gboolean with_flush
>  
>      if (s->uncork_op) {
>          pa_operation_cancel(s->uncork_op);
> -        pa_operation_unref(s->uncork_op);
> -        s->uncork_op = NULL;
> +        g_clear_pointer(&s->uncork_op, pa_operation_unref);
>      }
>  
>      if (!pa_stream_is_corked(s->stream) && !s->cork_op) {
> @@ -288,8 +272,7 @@ static void stream_stop(SpicePulse *pulse, struct stream *s)
>          g_warning("pa_stream_disconnect() failed: %s",
>                    pa_strerror(pa_context_errno(p->context)));
>      }
> -    pa_stream_unref(s->stream);
> -    s->stream = NULL;
> +    g_clear_pointer(&s->stream, pa_stream_unref);
>  }
>  
>  static void stream_state_callback(pa_stream *s, void *userdata)
> @@ -928,8 +911,7 @@ static gboolean free_async_task(gpointer user_data)
>  
>      if (task->pa_op != NULL) {
>          pa_operation_cancel(task->pa_op);
> -        pa_operation_unref(task->pa_op);
> -        task->pa_op = NULL;
> +        g_clear_pointer(&task->pa_op, pa_operation_unref);
>      }
>  
>      if (task->pulse) {
> @@ -966,8 +948,7 @@ static void cancel_task(GCancellable *cancellable, gpointer user_data)
>       * cancelled task operation before free_async_task is called */
>      if (task->pa_op != NULL) {
>          pa_operation_cancel(task->pa_op);
> -        pa_operation_unref(task->pa_op);
> -        task->pa_op = NULL;
> +        g_clear_pointer(&task->pa_op, pa_operation_unref);
>      }
>  
>      /* Clear the pending_restore_task reference to avoid triggering a
> @@ -1034,8 +1015,7 @@ static void spice_pulse_complete_all_async_tasks(SpicePulse *pulse, const gchar
>          complete_task(pulse, task, err_msg);
>          free_async_task(task);
>      }
> -    g_list_free(p->results);
> -    p->results = NULL;
> +    g_clear_pointer(&p->results, g_list_free);
>      SPICE_DEBUG("All async tasks completed");
>  }
>  
> diff --git a/src/spice-session.c b/src/spice-session.c
> index 9bf3f40..e02d684 100644
> --- a/src/spice-session.c
> +++ b/src/spice-session.c
> @@ -317,8 +317,7 @@ session_disconnect(SpiceSession *self, gboolean keep_main)
>  
>      s->connection_id = 0;
>  
> -    g_free(s->name);
> -    s->name = NULL;
> +    g_clear_pointer(&s->name, g_free);
>      memset(s->uuid, 0, sizeof(s->uuid));
>  
>      spice_session_abort_migration(self);
> @@ -1762,11 +1761,9 @@ void spice_session_abort_migration(SpiceSession *session)
>      }
>  
>  end:
> -    g_list_free(s->migration_left);
> -    s->migration_left = NULL;
> +    g_clear_pointer(&s->migration_left, g_list_free);
>      session_disconnect(s->migration, FALSE);
> -    g_object_unref(s->migration);
> -    s->migration = NULL;
> +    g_clear_pointer(&s->migration, g_object_unref);
>  
>      s->migrate_wait_init = FALSE;
>      if (s->after_main_init) {
> @@ -1805,8 +1802,7 @@ void spice_session_channel_migrate(SpiceSession *session, SpiceChannel *channel)
>      if (g_list_length(s->migration_left) == 0) {
>          CHANNEL_DEBUG(channel, "migration: all channel migrated, success");
>          session_disconnect(s->migration, FALSE);
> -        g_object_unref(s->migration);
> -        s->migration = NULL;
> +        g_clear_pointer(&s->migration, g_object_unref);
>          spice_session_set_migration_state(session, SPICE_SESSION_MIGRATION_NONE);
>      }
>  }
> diff --git a/src/spice-widget-cairo.c b/src/spice-widget-cairo.c
> index 8c45524..5aaabb0 100644
> --- a/src/spice-widget-cairo.c
> +++ b/src/spice-widget-cairo.c
> @@ -53,13 +53,9 @@ void spicex_image_destroy(SpiceDisplay *display)
>  {
>      SpiceDisplayPrivate *d = display->priv;
>  
> -    if (d->ximage) {
> -        cairo_surface_destroy(d->ximage);
> -        d->ximage = NULL;
> -    }
> -    if (d->convert && d->data) {
> -        g_free(d->data);
> -        d->data = NULL;
> +    g_clear_pointer(&d->ximage, cairo_surface_destroy);
> +    if (d->convert) {
> +        g_clear_pointer(&d->data, g_free);
>      }

Same as above.

>      d->convert = FALSE;
>  }
> diff --git a/src/spice-widget.c b/src/spice-widget.c
> index 72a0355..f30b80f 100644
> --- a/src/spice-widget.c
> +++ b/src/spice-widget.c
> @@ -432,12 +432,8 @@ static void spice_display_finalize(GObject *obj)
>  
>      SPICE_DEBUG("Finalize spice display");
>  
> -    if (d->grabseq) {
> -        spice_grab_sequence_free(d->grabseq);
> -        d->grabseq = NULL;
> -    }
> -    g_free(d->activeseq);
> -    d->activeseq = NULL;
> +    g_clear_pointer(&d->grabseq, spice_grab_sequence_free);
> +    g_clear_pointer(&d->activeseq, g_free);
>  
>      g_clear_object(&d->show_cursor);
>      g_clear_object(&d->mouse_cursor);
> @@ -825,10 +821,7 @@ static void try_keyboard_ungrab(SpiceDisplay *display)
>      SPICE_DEBUG("ungrab keyboard");
>      gdk_keyboard_ungrab(GDK_CURRENT_TIME);
>  #ifdef G_OS_WIN32
> -    if (d->keyboard_hook != NULL) {
> -        UnhookWindowsHookEx(d->keyboard_hook);
> -        d->keyboard_hook = NULL;
> -    }
> +    g_clear_pointer(&d->keyboard_hook, UnhookWindowsHookEx);
>  #endif
>      d->keyboard_grab_active = false;
>      g_signal_emit(widget, signals[SPICE_DISPLAY_KEYBOARD_GRAB], 0, false);
> @@ -2354,8 +2347,7 @@ static void cursor_set(SpiceCursorChannel *channel,
>  #endif
>      if (d->show_cursor) {
>          /* unhide */
> -        g_object_unref(d->show_cursor);
> -        d->show_cursor = NULL;
> +        g_clear_pointer(&d->show_cursor, g_object_unref);
>          if (d->mouse_mode == SPICE_MOUSE_MODE_SERVER) {
>              /* keep a hidden cursor, will be shown in cursor_move() */
>              d->show_cursor = cursor;
> diff --git a/src/usb-acl-helper.c b/src/usb-acl-helper.c
> index 487e1ee..fa845be 100644
> --- a/src/usb-acl-helper.c
> +++ b/src/usb-acl-helper.c
> @@ -58,15 +58,8 @@ static void spice_usb_acl_helper_cleanup(SpiceUsbAclHelper *self)
>  
>      g_clear_object(&priv->task);
>  
> -    if (priv->in_ch) {
> -        g_io_channel_unref(priv->in_ch);
> -        priv->in_ch = NULL;
> -    }
> -
> -    if (priv->out_ch) {
> -        g_io_channel_unref(priv->out_ch);
> -        priv->out_ch = NULL;
> -    }
> +    g_clear_pointer(&priv->in_ch, g_io_channel_unref);
> +    g_clear_pointer(&priv->out_ch, g_io_channel_unref);
>  }
>  
>  static void spice_usb_acl_helper_finalize(GObject *gobject)
> diff --git a/src/wocky-http-proxy.c b/src/wocky-http-proxy.c
> index 33d57d8..f62f1fb 100644
> --- a/src/wocky-http-proxy.c
> +++ b/src/wocky-http-proxy.c
> @@ -416,8 +416,7 @@ request_write_cb (GObject *source,
>  
>     if (data->offset == data->length)
>      {
> -      g_free (data->buffer);
> -      data->buffer = NULL;
> +      g_clear_pointer(&data->buffer, g_free);
>  
>        g_data_input_stream_read_until_async (data->data_in,
>            HTTP_END_MARKER,
> 

-- 
Eduardo de Barros Lima (Etrunko)
Software Engineer - RedHat
etrunko at redhat.com


More information about the Spice-devel mailing list