[libgovirt] Use g_clear_{object,pointer} where appropriate



commit 29ed31b3bd36fb777bfaad6beeb2b6942732d1a3
Author: Eduardo Lima (Etrunko) <etrunko redhat com>
Date:   Wed Aug 28 11:46:46 2019 -0300

    Use g_clear_{object,pointer} where appropriate
    
    Signed-off-by: Eduardo Lima (Etrunko) <etrunko redhat com>

 govirt/ovirt-collection.c | 11 +++--------
 govirt/ovirt-proxy.c      | 47 ++++++++++++-----------------------------------
 govirt/ovirt-resource.c   | 11 +++--------
 govirt/ovirt-vm.c         |  3 +--
 4 files changed, 19 insertions(+), 53 deletions(-)
---
diff --git a/govirt/ovirt-collection.c b/govirt/ovirt-collection.c
index fc5d101..9ebe230 100644
--- a/govirt/ovirt-collection.c
+++ b/govirt/ovirt-collection.c
@@ -110,9 +110,7 @@ static void ovirt_collection_finalize(GObject *object)
 {
     OvirtCollection *collection = OVIRT_COLLECTION(object);
 
-    if (collection->priv->resources != NULL) {
-        g_hash_table_unref(collection->priv->resources);
-    }
+    g_clear_pointer(&collection->priv->resources, g_hash_table_unref);
     g_free(collection->priv->href);
     g_free(collection->priv->collection_xml_name);
     g_free(collection->priv->resource_xml_name);
@@ -212,13 +210,10 @@ void ovirt_collection_set_resources(OvirtCollection *collection, GHashTable *res
 {
     g_return_if_fail(OVIRT_IS_COLLECTION(collection));
 
-    if (collection->priv->resources != NULL) {
-        g_hash_table_unref(collection->priv->resources);
-    }
+    g_clear_pointer(&collection->priv->resources, g_hash_table_unref);
+
     if (resources != NULL) {
         collection->priv->resources = g_hash_table_ref(resources);
-    } else {
-        collection->priv->resources = NULL;
     }
 
     g_object_notify(G_OBJECT(collection), "resources");
diff --git a/govirt/ovirt-proxy.c b/govirt/ovirt-proxy.c
index ad00b8c..d335825 100644
--- a/govirt/ovirt-proxy.c
+++ b/govirt/ovirt-proxy.c
@@ -207,12 +207,10 @@ static void ovirt_proxy_call_async_data_free(OvirtProxyCallAsyncData *data)
         if (data->destroy_call_data != NULL) {
             data->destroy_call_data(data->call_user_data);
         }
-        if (data->proxy != NULL) {
-            g_object_unref(G_OBJECT(data->proxy));
-        }
-        if (data->result != NULL) {
-            g_object_unref(G_OBJECT(data->result));
-        }
+
+        g_clear_object(&data->proxy);
+        g_clear_object(&data->result);
+
         if ((data->cancellable != NULL) && (data->cancellable_cb_id != 0)) {
             if (g_cancellable_is_cancelled(data->cancellable)) {
                 /* Cancellable has already been cancelled, we don't need to use
@@ -513,12 +511,8 @@ static char *write_to_tmp_file(const char *template,
     result = g_file_get_path(tmp_file);
 
 end:
-    if (tmp_file != NULL) {
-        g_object_unref(G_OBJECT(tmp_file));
-    }
-    if (iostream != NULL) {
-        g_object_unref(G_OBJECT(iostream));
-    }
+    g_clear_object(&tmp_file);
+    g_clear_object(&iostream);
 
     return result;
 }
@@ -644,8 +638,7 @@ gboolean ovirt_proxy_fetch_ca_certificate(OvirtProxy *proxy, GError **error)
     set_display_ca_cert_from_data(proxy, cert_data, cert_length);
 
 error:
-    if (source != NULL)
-        g_object_unref(source);
+    g_clear_object(&source);
 
     return load_ok;
 }
@@ -837,24 +830,10 @@ ovirt_proxy_dispose(GObject *obj)
 {
     OvirtProxy *proxy = OVIRT_PROXY(obj);
 
-    if (proxy->priv->cookie_jar) {
-        g_object_unref(G_OBJECT(proxy->priv->cookie_jar));
-        proxy->priv->cookie_jar = NULL;
-    }
-
-    g_warn_if_fail(proxy->priv->additional_headers != NULL);
-    g_hash_table_unref(proxy->priv->additional_headers);
-    proxy->priv->additional_headers = NULL;
-
-    if (proxy->priv->api != NULL) {
-        g_object_unref(proxy->priv->api);
-        proxy->priv->api = NULL;
-    }
-
-    if (proxy->priv->display_ca != NULL) {
-        g_byte_array_unref(proxy->priv->display_ca);
-        proxy->priv->display_ca = NULL;
-    }
+    g_clear_object(&proxy->priv->cookie_jar);
+    g_clear_pointer(&proxy->priv->additional_headers, g_hash_table_unref);
+    g_clear_object(&proxy->priv->api);
+    g_clear_pointer(&proxy->priv->display_ca, g_byte_array_unref);
 
     G_OBJECT_CLASS(ovirt_proxy_parent_class)->dispose(obj);
 }
@@ -1164,9 +1143,7 @@ static void ovirt_proxy_set_api_from_xml(OvirtProxy *proxy,
 {
     OvirtCollection *vms;
 
-    if (proxy->priv->api != NULL) {
-        g_object_unref(G_OBJECT(proxy->priv->api));
-    }
+    g_clear_object(&proxy->priv->api);
     proxy->priv->api = ovirt_api_new_from_xml(node, error);
 
     vms = ovirt_api_get_vms(proxy->priv->api);
diff --git a/govirt/ovirt-resource.c b/govirt/ovirt-resource.c
index cc670db..8a3ae56 100644
--- a/govirt/ovirt-resource.c
+++ b/govirt/ovirt-resource.c
@@ -141,14 +141,9 @@ static void ovirt_resource_dispose(GObject *object)
 {
     OvirtResource *resource = OVIRT_RESOURCE(object);
 
-    if (resource->priv->actions != NULL) {
-        g_hash_table_unref(resource->priv->actions);
-        resource->priv->actions = NULL;
-    }
-    if (resource->priv->sub_collections != NULL) {
-        g_hash_table_unref(resource->priv->sub_collections);
-        resource->priv->sub_collections = NULL;
-    }
+    g_clear_pointer(&resource->priv->actions, g_hash_table_unref);
+    g_clear_pointer(&resource->priv->sub_collections, g_hash_table_unref);
+
     if (resource->priv->xml != NULL) {
         g_boxed_free(REST_TYPE_XML_NODE, resource->priv->xml);
         resource->priv->xml = NULL;
diff --git a/govirt/ovirt-vm.c b/govirt/ovirt-vm.c
index 0dc0e60..697cac8 100644
--- a/govirt/ovirt-vm.c
+++ b/govirt/ovirt-vm.c
@@ -133,8 +133,7 @@ static void ovirt_vm_set_property(GObject *object,
         vm->priv->state = g_value_get_enum(value);
         break;
     case PROP_DISPLAY:
-        if (vm->priv->display != NULL)
-            g_object_unref(vm->priv->display);
+        g_clear_object(&vm->priv->display);
         vm->priv->display = g_value_dup_object(value);
         break;
     case PROP_HOST_HREF:


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