[grilo-plugins] all: Use g_clear_pointer/object when possible



commit 6eea596179a834ef0e0a3b5103554e9d2a4ce705
Author: Juan A. Suarez Romero <jasuarez igalia com>
Date:   Wed Mar 5 19:40:46 2014 +0100

    all: Use g_clear_pointer/object when possible
    
    Reduces the number of lines and makes the code more readable.

 src/apple-trailers/grl-apple-trailers.c      |   13 ++------
 src/bliptv/grl-bliptv.c                      |   26 ++++++-----------
 src/bookmarks/grl-bookmarks.c                |   14 ++------
 src/filesystem/grl-filesystem.c              |   16 +++-------
 src/flickr/grl-flickr.c                      |   25 ++++------------
 src/jamendo/grl-jamendo.c                    |   19 +++---------
 src/lastfm-albumart/grl-lastfm-albumart.c    |   10 ++----
 src/local-metadata/grl-local-metadata.c      |    7 +---
 src/lua-factory/grl-lua-factory.c            |   24 ++++-----------
 src/magnatune/grl-magnatune.c                |    3 +-
 src/metadata-store/grl-metadata-store.c      |    7 +---
 src/optical-media/grl-optical-media.c        |    3 +-
 src/podcasts/grl-podcasts.c                  |   36 +++++++----------------
 src/raitv/grl-raitv.c                        |   33 ++++++----------------
 src/shoutcast/grl-shoutcast.c                |   39 ++++++--------------------
 src/tmdb/grl-tmdb-request.c                  |   30 +++----------------
 src/tracker/grl-tracker-request-queue.c      |    4 +--
 src/tracker/grl-tracker-source-api.c         |   12 ++-----
 src/tracker/grl-tracker-source-notif.c       |    6 +--
 src/tracker/grl-tracker-source.c             |   10 ++----
 src/tracker/grl-tracker.c                    |    3 +-
 src/upnp/grl-upnp.c                          |   19 +++---------
 src/vimeo/grl-vimeo.c                        |    7 +---
 src/vimeo/gvimeo.c                           |    5 +--
 src/youtube/grl-youtube.c                    |   30 +++++--------------
 tests/tmdb/test_tmdb_fast_resolution.c       |    7 +---
 tests/tmdb/test_tmdb_fast_resolution_by_id.c |    7 +---
 tests/tmdb/test_tmdb_full_resolution.c       |    7 +---
 tests/tmdb/test_tmdb_missing_configuration.c |   11 ++-----
 tests/tmdb/test_tmdb_preconditions.c         |    7 +---
 30 files changed, 121 insertions(+), 319 deletions(-)
---
diff --git a/src/apple-trailers/grl-apple-trailers.c b/src/apple-trailers/grl-apple-trailers.c
index 9b3deb4..6778118 100644
--- a/src/apple-trailers/grl-apple-trailers.c
+++ b/src/apple-trailers/grl-apple-trailers.c
@@ -195,8 +195,7 @@ grl_apple_trailers_source_finalize (GObject *object)
 
   self = GRL_APPLE_TRAILERS_SOURCE (object);
 
-  if (self->priv->wc)
-    g_object_unref (self->priv->wc);
+  g_clear_object (&self->priv->wc);
 
   G_OBJECT_CLASS (grl_apple_trailers_source_parent_class)->finalize (object);
 }
@@ -505,14 +504,8 @@ xml_parse_result (const gchar *str, OperationData *op_data)
                          op_data->bs->user_data,
                          error);
 
-  if (op_data->xml_doc) {
-    xmlFreeDoc (op_data->xml_doc);
-  }
-
-  if (error) {
-    g_error_free (error);
-  }
-
+  g_clear_pointer (&op_data->xml_doc, (GDestroyNotify) xmlFreeDoc);
+  g_clear_error (&error);
   g_object_unref (op_data->cancellable);
   g_slice_free (OperationData, op_data);
 }
diff --git a/src/bliptv/grl-bliptv.c b/src/bliptv/grl-bliptv.c
index f047d7b..6a5d3db 100644
--- a/src/bliptv/grl-bliptv.c
+++ b/src/bliptv/grl-bliptv.c
@@ -170,10 +170,8 @@ grl_bliptv_source_dispose (GObject *object)
   GrlBliptvSource *self;
 
   self= GRL_BLIPTV_SOURCE (object);
-  if (self->priv->wc) {
-    g_object_unref (self->priv->wc);
-    self->priv->wc = NULL;
-  }
+
+  g_clear_object (&self->priv->wc);
 
   G_OBJECT_CLASS (grl_bliptv_source_parent_class)->dispose (object);
 }
@@ -249,12 +247,10 @@ bliptv_setup_mapping (void)
 static void
 bliptv_operation_free (BliptvOperation *op)
 {
-  if (op->cancellable)
-    g_object_unref (op->cancellable);
-  if (op->source)
-    g_object_unref (op->source);
-  if (op->url)
-    g_free (op->url);
+  g_clear_object (&op->cancellable);
+  g_clear_object (&op->source);
+  g_clear_pointer (&op->url, (GDestroyNotify) g_free);
+
   g_slice_free (BliptvOperation, op);
 }
 
@@ -319,9 +315,7 @@ call_raw_async_cb (GObject *     source_object,
   if (nb_items == 1) {
     obj = xmlXPathEvalExpression ((xmlChar *) "string(/rss/channel/item[0]/blip:item_id)", xpath);
     if (!obj || !obj->stringval || obj->stringval[0] == '\0') {
-      if (obj) {
-        xmlXPathFreeObject (obj);
-      }
+      g_clear_pointer (&obj, (GDestroyNotify) xmlXPathFreeObject);
       nb_items = 0;
       goto finalize_send_last;
     } else {
@@ -439,10 +433,8 @@ call_raw_async_cb (GObject *     source_object,
   }
 
  finalize_free:
-  if (xpath)
-    xmlXPathFreeContext (xpath);
-  if (doc)
-    xmlFreeDoc (doc);
+  g_clear_pointer (&xpath, (GDestroyNotify) xmlXPathFreeContext);
+  g_clear_pointer (&doc, (GDestroyNotify) xmlFreeDoc);
 }
 
 /* ================== API Implementation ================ */
diff --git a/src/bookmarks/grl-bookmarks.c b/src/bookmarks/grl-bookmarks.c
index c9c414f..ecd62b2 100644
--- a/src/bookmarks/grl-bookmarks.c
+++ b/src/bookmarks/grl-bookmarks.c
@@ -302,8 +302,7 @@ grl_bookmarks_source_init (GrlBookmarksSource *source)
   if (r) {
     if (sql_error) {
       GRL_WARNING ("Failed to create database tables: %s", sql_error);
-      sqlite3_free (sql_error);
-      sql_error = NULL;
+      g_clear_pointer (&sql_error, (GDestroyNotify) sqlite3_free);
     } else {
       GRL_WARNING ("Failed to create database tables.");
     }
@@ -537,8 +536,7 @@ produce_bookmarks_from_sql (OperationSpec *os, const gchar *sql)
   }
 
  free_resources:
-  if (sql_stmt)
-    sqlite3_finalize (sql_stmt);
+  g_clear_pointer (&sql_stmt, (GDestroyNotify) sqlite3_finalize);
 }
 
 static void
@@ -874,9 +872,7 @@ grl_bookmarks_source_store (GrlSource *source, GrlSourceStoreSpec *ss)
   store_bookmark (GRL_BOOKMARKS_SOURCE (ss->source),
                   &keylist, ss->parent, ss->media, &error);
   ss->callback (ss->source, ss->media, keylist, ss->user_data, error);
-  if (error) {
-    g_error_free (error);
-  }
+  g_clear_error (&error);
 }
 
 static void grl_bookmarks_source_remove (GrlSource *source,
@@ -887,9 +883,7 @@ static void grl_bookmarks_source_remove (GrlSource *source,
   remove_bookmark (GRL_BOOKMARKS_SOURCE (rs->source),
                   rs->media_id, &error);
   rs->callback (rs->source, rs->media, rs->user_data, error);
-  if (error) {
-    g_error_free (error);
-  }
+  g_clear_error (&error);
 }
 
 static void
diff --git a/src/filesystem/grl-filesystem.c b/src/filesystem/grl-filesystem.c
index 143f0e6..0efcd52 100644
--- a/src/filesystem/grl-filesystem.c
+++ b/src/filesystem/grl-filesystem.c
@@ -392,12 +392,10 @@ file_is_valid_content (GFileInfo *info, gboolean fast, GrlOperationOptions *opti
   }
 
  end:
-  if (file_date)
-    g_date_time_unref (file_date);
-  if (min_date)
-    g_date_time_unref (min_date);
-  if (max_date)
-    g_date_time_unref (max_date);
+  g_clear_pointer (&file_date, g_date_time_unref);
+  g_clear_pointer (&min_date, g_date_time_unref);
+  g_clear_pointer (&max_date, g_date_time_unref);
+
   return is_media;
 }
 
@@ -916,11 +914,7 @@ notify_parent_change (GrlSource *source, GFile *child, GrlSourceChangeType chang
   media = grl_pls_file_to_media (NULL, parent ? parent : child, NULL, fs_source->priv->handle_pls, options);
   grl_source_notify_change (source, media, change, FALSE);
   g_object_unref (media);
-
-  if (parent) {
-    g_object_unref (parent);
-  }
-
+  g_clear_object (&parent);
   g_object_unref (options);
 }
 
diff --git a/src/flickr/grl-flickr.c b/src/flickr/grl-flickr.c
index 15dc6b0..268228b 100644
--- a/src/flickr/grl-flickr.c
+++ b/src/flickr/grl-flickr.c
@@ -237,14 +237,10 @@ grl_flickr_plugin_init (GrlRegistry *registry,
                                     NULL);
     }
 
-    if (flickr_key != NULL)
-      g_free (flickr_key);
-    if (flickr_token != NULL)
-      g_free (flickr_token);
-    if (flickr_secret != NULL)
-      g_free (flickr_secret);
-    if (flickr_token_secret != NULL)
-      g_free (flickr_token_secret);
+    g_clear_pointer (&flickr_key, g_free);
+    g_clear_pointer (&flickr_token, g_free);
+    g_clear_pointer (&flickr_secret, g_free);
+    g_clear_pointer (&flickr_token_secret, g_free);
 
     configs = g_list_next (configs);
   }
@@ -827,17 +823,8 @@ grl_flickr_get_goa_multiple_config (GrlPlugin *plugin, gboolean public)
           grl_config_set_api_token (conf, access_token);
           grl_config_set_api_token_secret (conf, token_secret);
 
-          if (access_token != NULL)
-          {
-            g_free(access_token);
-            access_token = NULL;
-          }
-
-          if (token_secret != NULL)
-          {
-            g_free(token_secret);
-            token_secret = NULL;
-          }
+          g_clear_pointer (&access_token, g_free);
+          g_clear_pointer (&token_secret, g_free);
         }
 
         configs = g_list_append (configs, conf);
diff --git a/src/jamendo/grl-jamendo.c b/src/jamendo/grl-jamendo.c
index 62f7a9b..cff3798 100644
--- a/src/jamendo/grl-jamendo.c
+++ b/src/jamendo/grl-jamendo.c
@@ -242,12 +242,9 @@ grl_jamendo_source_finalize (GObject *object)
   GrlJamendoSource *self;
 
   self = GRL_JAMENDO_SOURCE (object);
-  if (self->priv->wc)
-    g_object_unref (self->priv->wc);
 
-  if (self->priv->cancellable
-      && G_IS_CANCELLABLE (self->priv->cancellable))
-    g_object_unref (self->priv->cancellable);
+  g_clear_object (&self->priv->wc);
+  g_clear_object (&self->priv->cancellable);
 
   G_OBJECT_CLASS (grl_jamendo_source_parent_class)->finalize (object);
 }
@@ -737,9 +734,7 @@ read_done_cb (GObject *source_object,
   }
 
   g_slice_free (XmlParseEntries, xpe);
-  if (error) {
-    g_error_free (error);
-  }
+  g_clear_error (&error);
 }
 
 static void
@@ -1059,9 +1054,7 @@ grl_jamendo_source_resolve (GrlSource *source,
     }
   }
 
-  if (id_split) {
-    g_strfreev (id_split);
-  }
+  g_clear_pointer (&id_split, g_clear_pointer);
 
   if (url) {
     xpe = g_slice_new0 (XmlParseEntries);
@@ -1209,9 +1202,7 @@ grl_jamendo_source_browse (GrlSource *source,
 
   read_url_async (GRL_JAMENDO_SOURCE (source), url, xpe);
   g_free (url);
-  if (container_split) {
-    g_strfreev (container_split);
-  }
+  g_clear_pointer (&container_split, g_strfreev);
 }
 
 /*
diff --git a/src/lastfm-albumart/grl-lastfm-albumart.c b/src/lastfm-albumart/grl-lastfm-albumart.c
index 34dd239..87b7220 100644
--- a/src/lastfm-albumart/grl-lastfm-albumart.c
+++ b/src/lastfm-albumart/grl-lastfm-albumart.c
@@ -150,8 +150,7 @@ G_DEFINE_TYPE (GrlLastfmAlbumartSource,
 static void
 grl_lastfm_albumart_source_finalize (GObject *object)
 {
-  if (wc && GRL_IS_NET_WC (wc))
-    g_object_unref (wc);
+  g_clear_object (&wc);
 
   G_OBJECT_CLASS (grl_lastfm_albumart_source_parent_class)->finalize (object);
 }
@@ -196,8 +195,7 @@ xml_get_image (const gchar *xmldata, const gchar *image_node)
   xmlFreeDoc (doc);
 
   if (g_strcmp0 (image, LASTFM_DEFAULT_IMAGE) == 0) {
-    g_free (image);
-    image = NULL;
+    g_clear_pointer (&image, g_free);
   }
 
   return image;
@@ -234,9 +232,7 @@ read_done_cb (GObject *source_object,
 
   /* Get rid of stored operation data */
   cancellable = grl_operation_get_data (rs->operation_id);
-  if (cancellable) {
-    g_object_unref (cancellable);
-  }
+  g_clear_object (&cancellable);
 
   if (!grl_net_wc_request_finish (GRL_NET_WC (source_object),
                               res,
diff --git a/src/local-metadata/grl-local-metadata.c b/src/local-metadata/grl-local-metadata.c
index 9c6c7b4..6830eea 100644
--- a/src/local-metadata/grl-local-metadata.c
+++ b/src/local-metadata/grl-local-metadata.c
@@ -436,9 +436,7 @@ got_file_info (GFile *file,
   /* Free stored operation data */
   cancellable = grl_operation_get_data (rs->operation_id);
 
-  if (cancellable) {
-    g_object_unref (cancellable);
-  }
+  g_clear_object (&cancellable);
 
   info = g_file_query_info_finish (file, result, &error);
   if (error)
@@ -488,8 +486,7 @@ error:
     }
 
 exit:
-  if (info)
-    g_object_unref (info);
+    g_clear_object (&info);
 }
 
 static void
diff --git a/src/lua-factory/grl-lua-factory.c b/src/lua-factory/grl-lua-factory.c
index 9ea2660..781224d 100644
--- a/src/lua-factory/grl-lua-factory.c
+++ b/src/lua-factory/grl-lua-factory.c
@@ -256,8 +256,7 @@ grl_lua_factory_source_new (gchar *lua_plugin_path,
                          NULL);
   g_free (source_name);
   g_free (source_desc);
-  if (source_tags)
-    g_strfreev (source_tags);
+  g_clear_pointer (&source_tags, g_strfreev);
 
   ret = lua_plugin_source_operations (L, source->priv->fn);
   if (ret != LUA_OK)
@@ -286,20 +285,15 @@ grl_lua_factory_source_new (gchar *lua_plugin_path,
   source->priv->l_st = L;
 
   if (lua_plugin_source_init (source) == FALSE) {
-    g_object_unref (G_OBJECT (source));
-    source = NULL;
+    g_clear_object (&source);
   }
 
   return source;
 
 bail:
   if (source != NULL) {
-    if (config_keys != NULL)
-      g_hash_table_unref (config_keys);
-
-    if (source->priv->configs != NULL)
-      g_object_unref (source->priv->configs);
-
+    g_clear_pointer (&config_keys, g_hash_table_unref);
+    g_clear_object (&source->priv->configs);
     g_list_free (source->priv->resolve_keys);
     g_list_free (source->priv->supported_keys);
     g_list_free (source->priv->slow_keys);
@@ -342,11 +336,8 @@ grl_lua_factory_source_finalize (GObject *object)
 {
   GrlLuaFactorySource *source = GRL_LUA_FACTORY_SOURCE (object);
 
-  if (source->priv->configs)
-    g_object_unref (source->priv->configs);
-
-  if (source->priv->config_keys)
-    g_hash_table_unref (source->priv->config_keys);
+  g_clear_object (&source->priv->configs);
+  g_clear_pointer (&source->priv->config_keys, g_hash_table_unref);
 
   g_list_free (source->priv->resolve_keys);
   g_list_free (source->priv->supported_keys);
@@ -779,8 +770,7 @@ lua_plugin_source_info (lua_State *L,
   if (lua_source_id == NULL
       || lua_source_name == NULL) {
     GRL_DEBUG ("Lua source info is not well defined.");
-    if (lua_source_tags)
-      g_strfreev (lua_source_tags);
+    g_clear_pointer (&lua_source_tags, g_strfreev);
     return !LUA_OK;
   }
 
diff --git a/src/magnatune/grl-magnatune.c b/src/magnatune/grl-magnatune.c
index 34eeacd..563d09f 100644
--- a/src/magnatune/grl-magnatune.c
+++ b/src/magnatune/grl-magnatune.c
@@ -867,8 +867,7 @@ end_browse:
     g_error_free(err);
   }
 
-  if (category_str_id != NULL)
-    g_free(category_str_id);
+  g_clear_pointer (&category_str_id, g_free);
 
   g_slice_free(OperationSpec, os);
 }
diff --git a/src/metadata-store/grl-metadata-store.c b/src/metadata-store/grl-metadata-store.c
index 0e07168..7304c21 100644
--- a/src/metadata-store/grl-metadata-store.c
+++ b/src/metadata-store/grl-metadata-store.c
@@ -248,8 +248,7 @@ grl_metadata_store_source_init (GrlMetadataStoreSource *source)
   if (r) {
     if (sql_error) {
       GRL_WARNING ("Failed to create database tables: %s", sql_error);
-      sqlite3_free (sql_error);
-      sql_error = NULL;
+      g_clear_pointer (&sql_error, sqlite3_free);
     } else {
       GRL_WARNING ("Failed to create database tables.");
     }
@@ -817,9 +816,7 @@ grl_metadata_store_source_store_metadata (GrlSource *source,
 
   sms->callback (sms->source, sms->media, failed_keys, sms->user_data, error);
 
-  if (error) {
-    g_error_free (error);
-  }
+  g_clear_error (&error);
   g_list_free (failed_keys);
 }
 
diff --git a/src/optical-media/grl-optical-media.c b/src/optical-media/grl-optical-media.c
index 350e8aa..4993d0d 100644
--- a/src/optical-media/grl-optical-media.c
+++ b/src/optical-media/grl-optical-media.c
@@ -199,8 +199,7 @@ grl_optical_media_source_finalize (GObject *object)
 
   g_list_free_full (source->priv->list, g_object_unref);
 
-  g_object_unref (source->priv->monitor);
-  source->priv->monitor = NULL;
+  g_clear_object (&source->priv->monitor);
 
   G_OBJECT_CLASS (grl_optical_media_source_parent_class)->finalize (object);
 }
diff --git a/src/podcasts/grl-podcasts.c b/src/podcasts/grl-podcasts.c
index 285aace..055a49e 100644
--- a/src/podcasts/grl-podcasts.c
+++ b/src/podcasts/grl-podcasts.c
@@ -399,8 +399,7 @@ grl_podcasts_source_init (GrlPodcastsSource *source)
   if (r) {
     if (sql_error) {
       GRL_WARNING ("Failed to create database tables: %s", sql_error);
-      sqlite3_free (sql_error);
-      sql_error = NULL;
+      g_clear_pointer (&sql_error, sqlite3_free);
     } else {
       GRL_WARNING ("Failed to create database tables.");
     }
@@ -421,8 +420,7 @@ grl_podcasts_source_finalize (GObject *object)
 
   source = GRL_PODCASTS_SOURCE (object);
 
-  if (source->priv->wc)
-    g_object_unref (source->priv->wc);
+  g_clear_object (&source->priv->wc);
 
   sqlite3_close (source->priv->db);
 
@@ -510,9 +508,7 @@ read_url_async (GrlPodcastsSource *source,
    * In this case, as we don't know the previous URL,
    * we ditch the Wc and create another. It's cheap.
    */
-  if (source->priv->wc)
-    g_object_unref (source->priv->wc);
-
+  g_clear_object (&source->priv->wc);
   source->priv->wc = grl_net_wc_new ();
   grl_net_wc_request_async (source->priv->wc, url, NULL, read_done_cb, arc);
 }
@@ -1085,8 +1081,7 @@ parse_entry (xmlDocPtr doc, xmlNodePtr entry, Entry *data)
         data->image = (gchar *) xmlGetProp (node, (xmlChar *) "href");
       }
     } else if (!xmlStrcmp (node->name, (const xmlChar *) "thumbnail")) {
-      if (data->image)
-        g_free (data->image);
+      g_clear_pointer (&data->image, g_free);
       data->image = (gchar *) xmlGetProp (node, (xmlChar *) "url");
     }
     node = node->next;
@@ -1340,14 +1335,10 @@ parse_feed (OperationSpec *os, const gchar *str, GError **error)
   return;
 
  free_resources:
-  if (podcast_data)
-    free_podcast_data (podcast_data);
-  if (xpathObj)
-    xmlXPathFreeObject (xpathObj);
-  if (xpathCtx)
-    xmlXPathFreeContext (xpathCtx);
-  if (doc)
-    xmlFreeDoc (doc);
+  g_clear_pointer (&podcast_data, free_podcast_data);
+  g_clear_pointer (&xpathObj, xmlXPathFreeObject);
+  g_clear_pointer (&xpathCtx, xmlXPathFreeContext);
+  g_clear_pointer (&doc, xmlFreeDoc);
 }
 
 static void
@@ -1534,8 +1525,7 @@ produce_podcasts (OperationSpec *os)
   }
 
  free_resources:
-  if (sql_stmt)
-    sqlite3_finalize (sql_stmt);
+  g_clear_pointer (&sql_stmt, sqlite3_finalize);
 }
 
 static void
@@ -1813,9 +1803,7 @@ grl_podcasts_source_store (GrlSource *source, GrlSourceStoreSpec *ss)
   }
 
   ss->callback (ss->source, ss->media, keylist, ss->user_data, error);
-  if (error) {
-    g_error_free (error);
-  }
+  g_clear_error (&error);
 }
 
 static void
@@ -1832,9 +1820,7 @@ grl_podcasts_source_remove (GrlSource *source,
                   rs->media_id, &error);
   }
   rs->callback (rs->source, rs->media, rs->user_data, error);
-  if (error) {
-    g_error_free (error);
-  }
+  g_clear_error (&error);
 }
 
 static gboolean
diff --git a/src/raitv/grl-raitv.c b/src/raitv/grl-raitv.c
index 3b70f03..f0ddc3b 100644
--- a/src/raitv/grl-raitv.c
+++ b/src/raitv/grl-raitv.c
@@ -284,10 +284,7 @@ grl_raitv_source_finalize (GObject *object)
 {
   GrlRaitvSource *source = GRL_RAITV_SOURCE (object);
 
-  if (source->priv->wc != NULL) {
-    g_object_unref (source->priv->wc);
-    source->priv->wc = NULL;
-  }
+  g_clear_object (&source->priv->wc);
 
   if (source->priv->raitv_search_mappings != NULL) {
     g_list_free_full (source->priv->raitv_search_mappings, g_free);
@@ -377,10 +374,8 @@ grl_raitv_source_init (GrlRaitvSource *self)
 static void
 raitv_operation_free (RaitvOperation *op)
 {
-  if (op->cancellable)
-    g_object_unref (op->cancellable);
-  if (op->source)
-    g_object_unref (op->source);
+  g_clear_object (&op->cancellable);
+  g_clear_object (&op->source);
   g_slice_free (RaitvOperation, op);
 }
 
@@ -555,11 +550,8 @@ proxy_call_search_grlnet_async_cb (GObject *source_object,
     }
 
  finalize:
-
-  if (xpath)
-    xmlXPathFreeContext (xpath);
-  if (doc)
-    xmlFreeDoc (doc);
+  g_clear_pointer (&xpath, xmlXPathFreeContext);
+  g_clear_pointer (&doc, xmlFreeDoc);
 
   /* Signal the last element if it was not already signaled */
   if (nb_items == 0 || g_bVideoNotFound) {
@@ -763,12 +755,8 @@ proxy_call_browse_grlnet_async_cb (GObject *source_object,
     }
 
  finalize:
-  //g_free (body);
-
-  if (xpath)
-    xmlXPathFreeContext (xpath);
-  if (doc)
-    xmlFreeDoc (doc);
+  g_clear_pointer (&xpath, xmlXPathFreeContext);
+  g_clear_pointer (&doc, xmlFreeDoc);
 
   /* Signal the last element if it was not already signaled */
   if (nb_items == 0) {
@@ -947,11 +935,8 @@ proxy_call_resolve_grlnet_async_cb (GObject *source_object,
                  op->user_data,
                  NULL);
 
-  if (xpath)
-    xmlXPathFreeContext (xpath);
-
-  if (doc)
-    xmlFreeDoc (doc);
+  g_clear_pointer (&xpath, xmlXPathFreeContext);
+  g_clear_pointer (&doc, xmlFreeDoc);
 }
 
 
diff --git a/src/shoutcast/grl-shoutcast.c b/src/shoutcast/grl-shoutcast.c
index 96b1fc6..0736720 100644
--- a/src/shoutcast/grl-shoutcast.c
+++ b/src/shoutcast/grl-shoutcast.c
@@ -223,20 +223,10 @@ grl_shoutcast_source_finalize (GObject *object)
 {
   GrlShoutcastSource *self = GRL_SHOUTCAST_SOURCE (object);
 
-  if (self->priv->wc && GRL_IS_NET_WC (self->priv->wc))
-    g_object_unref (self->priv->wc);
-
-  if (self->priv->cancellable && G_IS_CANCELLABLE (self->priv->cancellable))
-    g_cancellable_cancel (self->priv->cancellable);
-
-  if (self->priv->cached_page) {
-    g_free (self->priv->cached_page);
-    self->priv->cached_page = NULL;
-  }
-
-  if (self->priv->dev_key) {
-    g_free (self->priv->dev_key);
-  }
+  g_clear_object (&self->priv->wc);
+  g_clear_pointer (&self->priv->cancellable, g_cancellable_cancel);
+  g_clear_pointer (&self->priv->cached_page, g_free);
+  g_clear_pointer (&self->priv->dev_key, g_free);
 
   G_OBJECT_CLASS (grl_shoutcast_source_parent_class)->finalize (object);
 }
@@ -336,9 +326,7 @@ build_media_from_station (OperationData *op_data)
   g_free (station_bitrate);
   g_free (media_id);
   g_free (media_url);
-  if (station_genres) {
-    g_strfreev (station_genres);
-  }
+  g_clear_pointer (&station_genres, g_strfreev);
 
   return media;
 }
@@ -463,9 +451,7 @@ xml_parse_result (const gchar *str, OperationData *op_data)
                              _("Cannot find media %s"),
                              grl_media_get_id (op_data->media));
       }
-      if (xpath_res) {
-        xmlXPathFreeObject (xpath_res);
-      }
+      g_clear_pointer (&xpath_res, xmlXPathFreeObject);
       xmlXPathFreeContext (xpath_ctx);
     } else {
       error = g_error_new_literal (GRL_CORE_ERROR,
@@ -522,17 +508,10 @@ xml_parse_result (const gchar *str, OperationData *op_data)
                       error);
 
  free_resources:
-  if (op_data->xml_doc) {
-    xmlFreeDoc (op_data->xml_doc);
-  }
-
-  if (op_data->filter_entry) {
-    g_free (op_data->filter_entry);
-  }
+  g_clear_pointer (&op_data->xml_doc, xmlFreeDoc);
+  g_clear_pointer (&op_data->filter_entry, g_free);
+  g_clear_error (&error);
 
-  if (error) {
-    g_error_free (error);
-  }
   g_slice_free (OperationData, op_data);
 }
 
diff --git a/src/tmdb/grl-tmdb-request.c b/src/tmdb/grl-tmdb-request.c
index 47a2b7b..0f59158 100644
--- a/src/tmdb/grl-tmdb-request.c
+++ b/src/tmdb/grl-tmdb-request.c
@@ -181,31 +181,11 @@ grl_tmdb_request_finalize (GObject *object)
   GrlTmdbRequest *self = GRL_TMDB_REQUEST (object);
 
   g_list_free (self->priv->details);
-
-  if (self->priv->api_key != NULL) {
-    g_free (self->priv->api_key);
-    self->priv->api_key = NULL;
-  }
-
-  if (self->priv->uri != NULL) {
-    g_free (self->priv->uri);
-    self->priv->uri = NULL;
-  }
-
-  if (self->priv->args != NULL) {
-    g_hash_table_unref (self->priv->args);
-    self->priv->args = NULL;
-  }
-
-  if (self->priv->base != NULL) {
-    soup_uri_free (self->priv->base);
-    self->priv->base = NULL;
-  }
-
-  if (self->priv->parser != NULL) {
-    g_object_unref (self->priv->parser);
-    self->priv->parser = NULL;
-  }
+  g_clear_pointer (&self->priv->api_key, g_free);
+  g_clear_pointer (&self->priv->uri, g_free);
+  g_clear_pointer (&self->priv->args, g_hash_table_unref);
+  g_clear_pointer (&self->priv->base, soup_uri_free);
+  g_clear_object (&self->priv->parser);
 
   G_OBJECT_CLASS (grl_tmdb_request_parent_class)->finalize (object);
 }
diff --git a/src/tracker/grl-tracker-request-queue.c b/src/tracker/grl-tracker-request-queue.c
index 0805f02..fb7d59e 100644
--- a/src/tracker/grl-tracker-request-queue.c
+++ b/src/tracker/grl-tracker-request-queue.c
@@ -42,9 +42,7 @@ grl_tracker_op_terminate (GrlTrackerOp *os)
   if (os == NULL)
     return;
 
-  if (os->cursor)
-    g_object_unref (os->cursor);
-
+  g_clear_object (&os->cursor);
   g_object_unref (os->cancel);
   g_free (os->request);
 
diff --git a/src/tracker/grl-tracker-source-api.c b/src/tracker/grl-tracker-source-api.c
index dfccb4e..ccd2250 100644
--- a/src/tracker/grl-tracker-source-api.c
+++ b/src/tracker/grl-tracker-source-api.c
@@ -482,8 +482,7 @@ tracker_resolve_cb (GObject      *source_object,
   }
 
  end_operation:
-  if (cursor)
-    g_object_unref (G_OBJECT (cursor));
+  g_clear_object (&cursor);
 
   grl_tracker_queue_done (grl_tracker_queue, os);
 }
@@ -727,12 +726,9 @@ grl_tracker_source_resolve (GrlSource *source,
 
   grl_tracker_queue_push (grl_tracker_queue, os);
 
-  if (sparql_type_filter != NULL)
-    g_free (sparql_type_filter);
-  if (constraint != NULL)
-    g_free (constraint);
-  if (sparql_select != NULL)
-    g_free (sparql_select);
+  g_clear_pointer (&sparql_type_filter, g_free);
+  g_clear_pointer (&constraint, g_free);
+  g_clear_pointer (&sparql_select, g_free);
 }
 
 gboolean
diff --git a/src/tracker/grl-tracker-source-notif.c b/src/tracker/grl-tracker-source-notif.c
index 6bf122a..c745fd5 100644
--- a/src/tracker/grl-tracker-source-notif.c
+++ b/src/tracker/grl-tracker-source-notif.c
@@ -194,8 +194,7 @@ tracker_evt_update_orphan_item_cb (GObject              *object,
       GRL_DEBUG ("\tend of parsing...");
     }
 
-    g_object_unref (evt->cursor);
-    evt->cursor = NULL;
+    g_clear_object (&evt->cursor);
 
     if (grl_tracker_per_device_source) {
       /* Once all items have been processed, add new sources and we're
@@ -429,8 +428,7 @@ tracker_evt_preupdate_sources_item_cb (GObject              *object,
       GRL_DEBUG ("\tend of parsing... start notifying sources");
     }
 
-    g_object_unref (evt->cursor);
-    evt->cursor = NULL;
+    g_clear_object (&evt->cursor);
 
     /* Once all sources have been preupdated, start items
        updates. */
diff --git a/src/tracker/grl-tracker-source.c b/src/tracker/grl-tracker-source.c
index 88777a2..bf06e30 100644
--- a/src/tracker/grl-tracker-source.c
+++ b/src/tracker/grl-tracker-source.c
@@ -152,8 +152,8 @@ grl_tracker_source_finalize (GObject *object)
   GrlTrackerSource *self;
 
   self = GRL_TRACKER_SOURCE (object);
-  if (self->priv->tracker_connection)
-    g_object_unref (self->priv->tracker_connection);
+
+  g_clear_object (&self->priv->tracker_connection);
 
   G_OBJECT_CLASS (grl_tracker_source_parent_class)->finalize (object);
 }
@@ -169,14 +169,12 @@ grl_tracker_source_set_property (GObject      *object,
 
   switch (propid) {
     case PROP_TRACKER_CONNECTION:
-      if (priv->tracker_connection != NULL)
-        g_object_unref (G_OBJECT (priv->tracker_connection));
+      g_clear_object (&priv->tracker_connection);
       priv->tracker_connection = g_object_ref (g_value_get_object (value));
       break;
 
     case PROP_TRACKER_DATASOURCE:
-      if (priv->tracker_datasource != NULL)
-        g_free (priv->tracker_datasource);
+      g_clear_pointer (&priv->tracker_datasource, g_free);
       priv->tracker_datasource = g_strdup (g_value_get_string (value));
       break;
 
diff --git a/src/tracker/grl-tracker.c b/src/tracker/grl-tracker.c
index 10c1473..8d8512d 100644
--- a/src/tracker/grl-tracker.c
+++ b/src/tracker/grl-tracker.c
@@ -168,8 +168,7 @@ tracker_get_upnp_class_cb (GObject      *object,
     }
   }
 
-  if (cursor)
-    g_object_unref (cursor);
+  g_clear_object (&cursor);
 
   if (grl_tracker_browse_filesystem)
     tracker_sparql_connection_query_async (grl_tracker_connection,
diff --git a/src/upnp/grl-upnp.c b/src/upnp/grl-upnp.c
index a60631a..acaecca 100644
--- a/src/upnp/grl-upnp.c
+++ b/src/upnp/grl-upnp.c
@@ -195,10 +195,7 @@ grl_upnp_plugin_deinit (GrlPlugin *plugin)
 {
   GRL_DEBUG ("grl_upnp_plugin_deinit");
 
-  if (context_manager != NULL) {
-    g_object_unref (context_manager);
-    context_manager = NULL;
-  }
+  g_clear_object (&context_manager);
 }
 
 GRL_PLUGIN_REGISTER (grl_upnp_plugin_init,
@@ -903,8 +900,7 @@ get_thumbnail (GList *nodes)
     if (is_http_get (node)) {
       counter++;
       if (is_image (node)) {
-        if (val)
-          g_free (val);
+        g_clear_pointer (&val, g_free);
         val = (gchar *) xmlNodeGetContent (node);
 
         if (has_thumbnail_marker (node))  /* that's definitely it! */
@@ -916,8 +912,7 @@ get_thumbnail (GList *nodes)
   if (val && counter == 1) {
     /* There was only one element with http-get protocol: that's the uri of the
      * media itself, not a thumbnail */
-    g_free (val);
-    val = NULL;
+    g_clear_pointer (&val, g_free);
   }
 
   return val;
@@ -1107,9 +1102,7 @@ gupnp_browse_cb (GUPnPServiceProxy *service,
     GRL_DEBUG ("Got no results");
     os->callback (os->source, os->operation_id,
                   NULL, 0, os->user_data, error? error: NULL);
-    if (error) {
-      g_error_free (error);
-    }
+    g_clear_error (&error);
 
     goto free_resources;
   }
@@ -1177,9 +1170,7 @@ gupnp_resolve_cb (GUPnPServiceProxy *service,
   if (!didl || !returned) {
     GRL_DEBUG ("Got no results for resolve");
     rs->callback (rs->source, rs->operation_id, rs->media, rs->user_data, error? error: NULL);
-    if (error) {
-      g_error_free (error);
-    }
+    g_clear_error (&error);
 
     goto free_resources;
   }
diff --git a/src/vimeo/grl-vimeo.c b/src/vimeo/grl-vimeo.c
index b6e40b5..1f60702 100644
--- a/src/vimeo/grl-vimeo.c
+++ b/src/vimeo/grl-vimeo.c
@@ -162,11 +162,8 @@ grl_vimeo_plugin_init (GrlRegistry *registry,
   init_result = TRUE;
 
  go_out:
-
-  if (vimeo_key != NULL)
-    g_free (vimeo_key);
-  if (vimeo_secret != NULL)
-    g_free (vimeo_secret);
+  g_clear_pointer (&vimeo_key, g_free);
+  g_clear_pointer (&vimeo_secret, g_free);
 
   return init_result;
 }
diff --git a/src/vimeo/gvimeo.c b/src/vimeo/gvimeo.c
index 1e89c30..16cc5b2 100644
--- a/src/vimeo/gvimeo.c
+++ b/src/vimeo/gvimeo.c
@@ -138,10 +138,7 @@ g_vimeo_dispose (GObject *object)
 {
   GVimeo *vimeo = G_VIMEO (object);
 
-  if (vimeo->priv->wc) {
-    g_object_unref (vimeo->priv->wc);
-    vimeo->priv->wc = NULL;
-  }
+  g_clear_object (&vimeo->priv->wc);
 
   G_OBJECT_CLASS (g_vimeo_parent_class)->dispose (object);
 }
diff --git a/src/youtube/grl-youtube.c b/src/youtube/grl-youtube.c
index 73352e6..a9dfa7d 100644
--- a/src/youtube/grl-youtube.c
+++ b/src/youtube/grl-youtube.c
@@ -420,11 +420,8 @@ grl_youtube_source_finalize (GObject *object)
 
   self = GRL_YOUTUBE_SOURCE (object);
 
-  if (self->priv->wc)
-    g_object_unref (self->priv->wc);
-
-  if (self->priv->service)
-    g_object_unref (self->priv->service);
+  g_clear_object (&self->priv->wc);
+  g_clear_object (&self->priv->service);
 
   G_OBJECT_CLASS (grl_youtube_source_parent_class)->finalize (object);
 }
@@ -445,9 +442,7 @@ release_operation_data (guint operation_id)
 {
   GCancellable *cancellable = grl_operation_get_data (operation_id);
 
-  if (cancellable) {
-    g_object_unref (cancellable);
-  }
+  g_clear_object (&cancellable);
 }
 
 static OperationSpec *
@@ -468,9 +463,7 @@ operation_spec_unref (OperationSpec *os)
 {
   os->ref_count--;
   if (os->ref_count == 0) {
-    if (os->cancellable) {
-      g_object_unref (os->cancellable);
-    }
+    g_clear_object (&os->cancellable);
     g_slice_free (OperationSpec, os);
     GRL_DEBUG ("freeing spec");
   }
@@ -862,9 +855,7 @@ resolve_cb (GObject *object,
                             rs);
   }
 
-  if (video) {
-    g_object_unref (video);
-  }
+  g_clear_object (&video);
 }
 
 static void
@@ -958,8 +949,7 @@ search_cb (GObject *object, GAsyncResult *result, OperationSpec *os)
     need_extra_unref = TRUE;
   }
 
-  if (feed)
-    g_object_unref (feed);
+  g_clear_object (&feed);
 
   GRL_DEBUG ("Unreffing spec in search_cb");
   operation_spec_unref (os);
@@ -1310,9 +1300,7 @@ media_from_uri_cb (GObject *object, GAsyncResult *result, gpointer user_data)
                            mfus);
   }
 
-  if (video) {
-    g_object_unref (video);
-  }
+  g_clear_object (&video);
 }
 
 static gboolean
@@ -1348,9 +1336,7 @@ produce_container_from_category_cb (BuildCategorySpec *spec)
   }
 
   rs->callback (rs->source, rs->operation_id, media, rs->user_data, error);
-  if (error) {
-    g_error_free (error);
-  }
+  g_clear_error (&error);
 
   return FALSE;
 }
diff --git a/tests/tmdb/test_tmdb_fast_resolution.c b/tests/tmdb/test_tmdb_fast_resolution.c
index 2202898..f8e9105 100644
--- a/tests/tmdb/test_tmdb_fast_resolution.c
+++ b/tests/tmdb/test_tmdb_fast_resolution.c
@@ -85,11 +85,8 @@ test_fast_resolution (void)
 
   g_assert (grl_media_get_publication_date (media) == NULL);
 
-  g_object_unref (media);
-  media = NULL;
-
-  g_object_unref (options);
-  options = NULL;
+  g_clear_object (&media);
+  g_clear_object (&options);
 
   test_shutdown_tmdb ();
 }
diff --git a/tests/tmdb/test_tmdb_fast_resolution_by_id.c b/tests/tmdb/test_tmdb_fast_resolution_by_id.c
index e177beb..22ab50b 100644
--- a/tests/tmdb/test_tmdb_fast_resolution_by_id.c
+++ b/tests/tmdb/test_tmdb_fast_resolution_by_id.c
@@ -87,11 +87,8 @@ test_fast_resolution_by_id (void)
 
   g_assert (grl_media_get_publication_date (media) == NULL);
 
-  g_object_unref (media);
-  media = NULL;
-
-  g_object_unref (options);
-  options = NULL;
+  g_clear_object (&media);
+  g_clear_object (&options);
 
   test_shutdown_tmdb ();
 }
diff --git a/tests/tmdb/test_tmdb_full_resolution.c b/tests/tmdb/test_tmdb_full_resolution.c
index b9676c0..dd8ce97 100644
--- a/tests/tmdb/test_tmdb_full_resolution.c
+++ b/tests/tmdb/test_tmdb_full_resolution.c
@@ -144,11 +144,8 @@ test_full_resolution (void)
   test_region_certificate (media, "DK", "15");
   test_region_certificate (media, "US", "PG-13");
 
-  g_object_unref (media);
-  media = NULL;
-
-  g_object_unref (options);
-  options = NULL;
+  g_clear_object (&media);
+  g_clear_object (&options);
 
   test_shutdown_tmdb ();
 }
diff --git a/tests/tmdb/test_tmdb_missing_configuration.c b/tests/tmdb/test_tmdb_missing_configuration.c
index 74fbef4..cd56c7e 100644
--- a/tests/tmdb/test_tmdb_missing_configuration.c
+++ b/tests/tmdb/test_tmdb_missing_configuration.c
@@ -55,14 +55,9 @@ test_missing_configuration (void)
    * file would have resulted in an error */
   g_assert (error != NULL);
 
-  g_object_unref (media);
-  media = NULL;
-
-  g_object_unref (options);
-  options = NULL;
-
-  g_error_free (error);
-  error = NULL;
+  g_clear_object (&media);
+  g_clear_object (&options);
+  g_clear_error (&error);
 
   test_shutdown_tmdb ();
 }
diff --git a/tests/tmdb/test_tmdb_preconditions.c b/tests/tmdb/test_tmdb_preconditions.c
index 2d6f866..73f0de7 100644
--- a/tests/tmdb/test_tmdb_preconditions.c
+++ b/tests/tmdb/test_tmdb_preconditions.c
@@ -78,11 +78,8 @@ test_preconditions (void)
                            &error);
   g_assert_no_error (error);
 
-  g_object_unref (media);
-  media = NULL;
-
-  g_object_unref (options);
-  options = NULL;
+  g_clear_object (&media);
+  g_clear_object (&options);
 
   test_shutdown_tmdb ();
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]