[PATCH 2/2] lib: Use g_clear_pointer() more

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

 



This change was generated using the following spatch:

  @ rule1 @
  expression a;
  identifier f;
  @@
    <...
  - f(*a);
    ... when != a;
  - *a = NULL;
  + g_clear_pointer(a, f);
    ...>

  @ rule2 @
  expression a;
  identifier f;
  @@
    <...
  - f(a);
    ... when != a;
  - a = NULL;
  + g_clear_pointer(&a, f);
    ...>

Then, I left some of the changes out, like tools/nss/ (which
doesn't link with glib) and put back a comment in
qemuBlockJobProcessEventCompletedActiveCommit() which coccinelle
decided to remove (I have no idea why).

Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx>
---
 src/ch/ch_driver.c                         |  6 +-
 src/ch/ch_process.c                        |  3 +-
 src/conf/capabilities.c                    |  9 +--
 src/conf/domain_conf.c                     | 49 ++++++----------
 src/conf/network_conf.c                    |  3 +-
 src/conf/numa_conf.c                       |  3 +-
 src/conf/nwfilter_conf.c                   |  3 +-
 src/conf/snapshot_conf.c                   |  3 +-
 src/conf/storage_source_conf.c             |  3 +-
 src/conf/virinterfaceobj.c                 |  3 +-
 src/conf/virnetworkobj.c                   |  6 +-
 src/conf/virnodedeviceobj.c                |  3 +-
 src/conf/virnwfilterbindingobj.c           |  3 +-
 src/conf/virsecretobj.c                    |  6 +-
 src/conf/virstorageobj.c                   |  6 +-
 src/datatypes.c                            |  6 +-
 src/hyperv/hyperv_wmi.c                    |  6 +-
 src/interface/interface_backend_netcf.c    | 12 ++--
 src/libvirt-domain.c                       |  3 +-
 src/libxl/libxl_capabilities.c             |  3 +-
 src/libxl/libxl_migration.c                |  6 +-
 src/locking/lock_daemon.c                  |  6 +-
 src/locking/lock_driver_lockd.c            |  3 +-
 src/logging/log_daemon.c                   |  6 +-
 src/lxc/lxc_controller.c                   | 12 ++--
 src/lxc/lxc_domain.c                       |  6 +-
 src/lxc/lxc_driver.c                       |  6 +-
 src/lxc/lxc_fuse.c                         |  3 +-
 src/lxc/lxc_native.c                       | 24 +++-----
 src/lxc/lxc_process.c                      | 21 +++----
 src/network/bridge_driver_linux.c          |  6 +-
 src/nwfilter/nwfilter_dhcpsnoop.c          |  6 +-
 src/nwfilter/nwfilter_driver.c             |  3 +-
 src/qemu/qemu_agent.c                      |  6 +-
 src/qemu/qemu_backup.c                     |  3 +-
 src/qemu/qemu_blockjob.c                   | 30 ++++------
 src/qemu/qemu_domain.c                     | 68 +++++++---------------
 src/qemu/qemu_domain_address.c             |  3 +-
 src/qemu/qemu_driver.c                     |  9 +--
 src/qemu/qemu_hotplug.c                    | 12 ++--
 src/qemu/qemu_migration.c                  |  9 +--
 src/qemu/qemu_monitor.c                    |  9 +--
 src/qemu/qemu_namespace.c                  |  3 +-
 src/qemu/qemu_process.c                    | 18 ++----
 src/remote/remote_daemon_dispatch.c        | 15 ++---
 src/remote/remote_driver.c                 | 21 +++----
 src/remote/remote_ssh_helper.c             |  3 +-
 src/rpc/virnetclient.c                     | 15 ++---
 src/rpc/virnetserverclient.c               | 15 ++---
 src/rpc/virnettlscontext.c                 |  3 +-
 src/secret/secret_driver.c                 |  6 +-
 src/security/security_manager.c            |  3 +-
 src/security/security_selinux.c            |  6 +-
 src/storage/storage_backend_iscsi_direct.c |  3 +-
 src/storage/storage_backend_rbd.c          |  6 +-
 src/storage/storage_util.c                 |  3 +-
 src/storage_file/storage_file_probe.c      |  3 +-
 src/storage_file/storage_source.c          |  3 +-
 src/test/test_driver.c                     |  6 +-
 src/util/viralloc.c                        |  3 +-
 src/util/virconf.c                         |  3 +-
 src/util/virerror.c                        |  3 +-
 src/util/vireventglib.c                    | 12 ++--
 src/util/virfile.c                         |  3 +-
 src/util/virfilecache.c                    |  6 +-
 src/util/virgdbus.c                        |  3 +-
 src/util/virmdev.c                         |  3 +-
 src/util/virnetdev.c                       |  3 +-
 src/util/virnetlink.c                      |  3 +-
 src/util/virpci.c                          |  3 +-
 src/util/virresctrl.c                      |  3 +-
 src/util/virstring.c                       |  3 +-
 src/util/virsysinfo.c                      | 18 ++----
 src/util/virtpm.c                          |  3 +-
 src/vbox/vbox_XPCOMCGlue.c                 |  3 +-
 src/vbox/vbox_common.c                     |  3 +-
 src/vbox/vbox_snapshot_conf.c              | 12 ++--
 src/vmx/vmx.c                              | 27 +++------
 src/vz/vz_driver.c                         |  3 +-
 src/vz/vz_sdk.c                            |  3 +-
 tests/commandtest.c                        |  6 +-
 tests/cputest.c                            |  3 +-
 tests/qemumonitortestutils.c               |  3 +-
 tests/virnetdaemontest.c                   |  3 +-
 tests/virnetsockettest.c                   |  3 +-
 tests/virnettlshelpers.c                   |  3 +-
 tests/virpcivpdtest.c                      |  2 +-
 tests/virusbtest.c                         |  6 +-
 tools/virsh-domain-monitor.c               |  6 +-
 tools/virsh-domain.c                       |  3 +-
 tools/virsh-interface.c                    |  3 +-
 tools/virsh-network.c                      |  6 +-
 tools/virsh-nodedev.c                      |  3 +-
 tools/virsh-nwfilter.c                     |  6 +-
 tools/virsh-pool.c                         |  3 +-
 tools/virsh-secret.c                       |  3 +-
 tools/virsh-snapshot.c                     | 16 ++---
 tools/virsh-volume.c                       |  3 +-
 tools/virsh.c                              |  3 +-
 tools/vsh.c                                | 18 ++----
 100 files changed, 252 insertions(+), 501 deletions(-)

diff --git a/src/ch/ch_driver.c b/src/ch/ch_driver.c
index 3223f31367..e751ddba19 100644
--- a/src/ch/ch_driver.c
+++ b/src/ch/ch_driver.c
@@ -871,8 +871,7 @@ static int chStateCleanup(void)
     virObjectUnref(ch_driver->caps);
     virObjectUnref(ch_driver->config);
     virMutexDestroy(&ch_driver->lock);
-    g_free(ch_driver);
-    ch_driver = NULL;
+    g_clear_pointer(&ch_driver, g_free);
 
     return 0;
 }
@@ -1401,8 +1400,7 @@ chDomainPinEmulator(virDomainPtr dom,
         if (virProcessSetAffinity(vm->pid, pcpumap, false) < 0)
             goto endjob;
 
-        virBitmapFree(def->cputune.emulatorpin);
-        def->cputune.emulatorpin = NULL;
+        g_clear_pointer(&def->cputune.emulatorpin, virBitmapFree);
 
         if (!(def->cputune.emulatorpin = virBitmapNewCopy(pcpumap)))
             goto endjob;
diff --git a/src/ch/ch_process.c b/src/ch/ch_process.c
index e4313d1008..6d9a286e8a 100644
--- a/src/ch/ch_process.c
+++ b/src/ch/ch_process.c
@@ -553,8 +553,7 @@ virCHProcessStop(virCHDriver *driver G_GNUC_UNUSED,
               vm->def->name, (int)vm->pid, (int)reason);
 
     if (priv->monitor) {
-        virCHMonitorClose(priv->monitor);
-        priv->monitor = NULL;
+        g_clear_pointer(&priv->monitor, virCHMonitorClose);
     }
 
  retry:
diff --git a/src/conf/capabilities.c b/src/conf/capabilities.c
index ef594d7241..169aabdee1 100644
--- a/src/conf/capabilities.c
+++ b/src/conf/capabilities.c
@@ -417,8 +417,7 @@ virCapabilitiesFreeMachines(virCapsGuestMachine **machines,
     if (!machines)
         return;
     for (i = 0; i < nmachines && machines[i]; i++) {
-        virCapabilitiesFreeGuestMachine(machines[i]);
-        machines[i] = NULL;
+        g_clear_pointer(&machines[i], virCapabilitiesFreeGuestMachine);
     }
     g_free(machines);
 }
@@ -2132,8 +2131,7 @@ virCapabilitiesInitResctrlMemory(virCaps *caps)
 
             VIR_APPEND_ELEMENT(caps->host.memBW.nodes, caps->host.memBW.nnodes, node);
         }
-        virCapsHostMemBWNodeFree(node);
-        node = NULL;
+        g_clear_pointer(&node, virCapsHostMemBWNodeFree);
     }
 
     if (virResctrlInfoGetMonitorPrefix(caps->host.resctrl, prefix,
@@ -2252,8 +2250,7 @@ virCapabilitiesInitCaches(virCaps *caps)
                 VIR_APPEND_ELEMENT(caps->host.cache.banks, caps->host.cache.nbanks, bank);
             }
 
-            virCapsHostCacheBankFree(bank);
-            bank = NULL;
+            g_clear_pointer(&bank, virCapsHostCacheBankFree);
         }
         if (rv < 0)
             goto cleanup;
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index d66496c9b7..ab8f2a52cc 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -3168,12 +3168,10 @@ static void
 virDomainHostdevSubsysSCSIClear(virDomainHostdevSubsysSCSI *scsisrc)
 {
     if (scsisrc->protocol == VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_ISCSI) {
-        virObjectUnref(scsisrc->u.iscsi.src);
-        scsisrc->u.iscsi.src = NULL;
+        g_clear_pointer(&scsisrc->u.iscsi.src, virObjectUnref);
     } else {
         VIR_FREE(scsisrc->u.host.adapter);
-        virObjectUnref(scsisrc->u.host.src);
-        scsisrc->u.host.src = NULL;
+        g_clear_pointer(&scsisrc->u.host.src, virObjectUnref);
     }
 }
 
@@ -3938,8 +3936,7 @@ virDomainObjEndAPI(virDomainObj **vm)
         return;
 
     virObjectUnlock(*vm);
-    virObjectUnref(*vm);
-    *vm = NULL;
+    g_clear_pointer(vm, virObjectUnref);
 }
 
 
@@ -4842,8 +4839,8 @@ virDomainDefPostParseMemtune(virDomainDef *def)
 
             nextBit = virBitmapNextSetBit(def->mem.hugepages[i].nodemask, 0);
             if (nextBit < 0) {
-                virBitmapFree(def->mem.hugepages[i].nodemask);
-                def->mem.hugepages[i].nodemask = NULL;
+                g_clear_pointer(&def->mem.hugepages[i].nodemask,
+                                virBitmapFree);
             }
         }
     }
@@ -4907,8 +4904,7 @@ virDomainDefAddConsoleCompat(virDomainDef *def)
             /* if the console source doesn't match */
             if (!virDomainChrSourceDefIsEqual(def->serials[0]->source,
                                               def->consoles[0]->source)) {
-                virDomainChrDefFree(def->consoles[0]);
-                def->consoles[0] = NULL;
+                g_clear_pointer(&def->consoles[0], virDomainChrDefFree);
             }
         }
 
@@ -5737,8 +5733,7 @@ virDomainDefRemoveOfflineVcpuPin(virDomainDef *def)
         vcpu = virDomainDefGetVcpu(def, i);
 
         if (vcpu && !vcpu->online && vcpu->cpumask) {
-            virBitmapFree(vcpu->cpumask);
-            vcpu->cpumask = NULL;
+            g_clear_pointer(&vcpu->cpumask, virBitmapFree);
 
             VIR_WARN("Ignoring unsupported vcpupin for offline vcpu '%zu'", i);
         }
@@ -10887,8 +10882,7 @@ virDomainNetDefParseXML(virDomainXMLOption *xmlopt,
     return def;
 
  error:
-    virDomainNetDefFree(def);
-    def = NULL;
+    g_clear_pointer(&def, virDomainNetDefFree);
     goto cleanup;
 }
 
@@ -11398,8 +11392,7 @@ virDomainChrSourceDefNew(virDomainXMLOption *xmlopt)
 
     if (xmlopt && xmlopt->privateData.chrSourceNew &&
         !(def->privateData = xmlopt->privateData.chrSourceNew())) {
-        virObjectUnref(def);
-        def = NULL;
+        g_clear_pointer(&def, virObjectUnref);
     }
 
     return def;
@@ -12773,8 +12766,7 @@ virDomainNetDefNew(virDomainXMLOption *xmlopt)
 
     if (xmlopt && xmlopt->privateData.networkNew &&
         !(def->privateData = xmlopt->privateData.networkNew())) {
-        virDomainNetDefFree(def);
-        def = NULL;
+        g_clear_pointer(&def, virDomainNetDefFree);
     }
 
     return def;
@@ -12828,8 +12820,7 @@ virDomainGraphicsDefParseXML(virDomainXMLOption *xmlopt,
     return def;
 
  error:
-    virDomainGraphicsDefFree(def);
-    def = NULL;
+    g_clear_pointer(&def, virDomainGraphicsDefFree);
     return NULL;
 }
 
@@ -13395,8 +13386,7 @@ virDomainRNGDefParseXML(virDomainXMLOption *xmlopt,
     return def;
 
  error:
-    virDomainRNGDefFree(def);
-    def = NULL;
+    g_clear_pointer(&def, virDomainRNGDefFree);
     return NULL;
 }
 
@@ -13601,8 +13591,7 @@ virSysinfoBIOSParseXML(xmlNodePtr node,
 
     if (!def->vendor && !def->version &&
         !def->date && !def->release) {
-        virSysinfoBIOSDefFree(def);
-        def = NULL;
+        g_clear_pointer(&def, virSysinfoBIOSDefFree);
     }
 
     *bios = g_steal_pointer(&def);
@@ -13675,8 +13664,7 @@ virSysinfoSystemParseXML(xmlNodePtr node,
 
     if (!def->manufacturer && !def->product && !def->version &&
         !def->serial && !def->uuid && !def->sku && !def->family) {
-        virSysinfoSystemDefFree(def);
-        def = NULL;
+        g_clear_pointer(&def, virSysinfoSystemDefFree);
     }
 
     *sysdef = g_steal_pointer(&def);
@@ -13806,8 +13794,7 @@ virSysinfoChassisParseXML(xmlNodePtr node,
 
     if (!def->manufacturer && !def->version &&
         !def->serial && !def->asset && !def->sku) {
-        virSysinfoChassisDefFree(def);
-        def = NULL;
+        g_clear_pointer(&def, virSysinfoChassisDefFree);
     }
 
     *chassisdef = g_steal_pointer(&def);
@@ -30588,8 +30575,7 @@ virDomainNetCreatePort(virConnectPtr conn,
         return -1;
 
     /* prepare to re-use portdef */
-    virNetworkPortDefFree(portdef);
-    portdef = NULL;
+    g_clear_pointer(&portdef, virNetworkPortDefFree);
 
     if (!(port = virNetworkPortCreateXML(net, portxml, flags)))
         return -1;
@@ -30992,8 +30978,7 @@ virDomainStorageSourceTranslateSourcePool(virStorageSource *src,
     virStorageNetHostDefFree(src->nhosts, src->hosts);
     src->nhosts = 0;
     src->hosts = NULL;
-    virStorageAuthDefFree(src->auth);
-    src->auth = NULL;
+    g_clear_pointer(&src->auth, virStorageAuthDefFree);
 
     switch ((virStoragePoolType) pooldef->type) {
     case VIR_STORAGE_POOL_DIR:
diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index c769bbaeb5..d8813ee2cc 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -118,9 +118,8 @@ virPortGroupDefClear(virPortGroupDef *def)
 {
     VIR_FREE(def->name);
     VIR_FREE(def->virtPortProfile);
-    virNetDevBandwidthFree(def->bandwidth);
+    g_clear_pointer(&def->bandwidth, virNetDevBandwidthFree);
     virNetDevVlanClear(&def->vlan);
-    def->bandwidth = NULL;
 }
 
 
diff --git a/src/conf/numa_conf.c b/src/conf/numa_conf.c
index 32821bf6a3..390ef49b84 100644
--- a/src/conf/numa_conf.c
+++ b/src/conf/numa_conf.c
@@ -554,8 +554,7 @@ virDomainNumatuneSet(virDomainNuma *numa,
     /* setting nodeset when placement auto is invalid */
     if (placement == VIR_DOMAIN_NUMATUNE_PLACEMENT_AUTO &&
         numa->memory.nodeset) {
-        virBitmapFree(numa->memory.nodeset);
-        numa->memory.nodeset = NULL;
+        g_clear_pointer(&numa->memory.nodeset, virBitmapFree);
     }
 
     if (placement != -1)
diff --git a/src/conf/nwfilter_conf.c b/src/conf/nwfilter_conf.c
index c886218dde..3e1d4f5927 100644
--- a/src/conf/nwfilter_conf.c
+++ b/src/conf/nwfilter_conf.c
@@ -2514,8 +2514,7 @@ virNWFilterRuleParse(xmlNodePtr node)
     return ret;
 
  err_exit:
-    virNWFilterRuleDefFree(ret);
-    ret = NULL;
+    g_clear_pointer(&ret, virNWFilterRuleDefFree);
     goto cleanup;
 }
 
diff --git a/src/conf/snapshot_conf.c b/src/conf/snapshot_conf.c
index 2d4c7190ba..79a7b891a2 100644
--- a/src/conf/snapshot_conf.c
+++ b/src/conf/snapshot_conf.c
@@ -94,8 +94,7 @@ static void
 virDomainSnapshotDiskDefClear(virDomainSnapshotDiskDef *disk)
 {
     VIR_FREE(disk->name);
-    virObjectUnref(disk->src);
-    disk->src = NULL;
+    g_clear_pointer(&disk->src, virObjectUnref);
 }
 
 void
diff --git a/src/conf/storage_source_conf.c b/src/conf/storage_source_conf.c
index 851a2da877..5c5422ab0c 100644
--- a/src/conf/storage_source_conf.c
+++ b/src/conf/storage_source_conf.c
@@ -1097,8 +1097,7 @@ virStorageSourceBackingStoreClear(virStorageSource *def)
     VIR_FREE(def->backingStoreRaw);
 
     /* recursively free backing chain */
-    virObjectUnref(def->backingStore);
-    def->backingStore = NULL;
+    g_clear_pointer(&def->backingStore, virObjectUnref);
 }
 
 
diff --git a/src/conf/virinterfaceobj.c b/src/conf/virinterfaceobj.c
index 9d75be6425..62b21b5e5f 100644
--- a/src/conf/virinterfaceobj.c
+++ b/src/conf/virinterfaceobj.c
@@ -103,8 +103,7 @@ virInterfaceObjEndAPI(virInterfaceObj **obj)
         return;
 
     virObjectUnlock(*obj);
-    virObjectUnref(*obj);
-    *obj = NULL;
+    g_clear_pointer(obj, virObjectUnref);
 }
 
 
diff --git a/src/conf/virnetworkobj.c b/src/conf/virnetworkobj.c
index f18eb35ae2..ddb325cdbd 100644
--- a/src/conf/virnetworkobj.c
+++ b/src/conf/virnetworkobj.c
@@ -131,8 +131,7 @@ virNetworkObjEndAPI(virNetworkObj **obj)
         return;
 
     virObjectUnlock(*obj);
-    virObjectUnref(*obj);
-    *obj = NULL;
+    g_clear_pointer(obj, virObjectUnref);
 }
 
 
@@ -250,8 +249,7 @@ virNetworkObjSetMacMap(virNetworkObj *obj,
 void
 virNetworkObjUnrefMacMap(virNetworkObj *obj)
 {
-    virObjectUnref(obj->macmap);
-    obj->macmap = NULL;
+    g_clear_pointer(&obj->macmap, virObjectUnref);
 }
 
 
diff --git a/src/conf/virnodedeviceobj.c b/src/conf/virnodedeviceobj.c
index 7a560349d4..ba84dce82b 100644
--- a/src/conf/virnodedeviceobj.c
+++ b/src/conf/virnodedeviceobj.c
@@ -108,8 +108,7 @@ virNodeDeviceObjEndAPI(virNodeDeviceObj **obj)
         return;
 
     virObjectUnlock(*obj);
-    virObjectUnref(*obj);
-    *obj = NULL;
+    g_clear_pointer(obj, virObjectUnref);
 }
 
 
diff --git a/src/conf/virnwfilterbindingobj.c b/src/conf/virnwfilterbindingobj.c
index d387af68c0..be43ae3931 100644
--- a/src/conf/virnwfilterbindingobj.c
+++ b/src/conf/virnwfilterbindingobj.c
@@ -134,8 +134,7 @@ virNWFilterBindingObjEndAPI(virNWFilterBindingObj **obj)
         return;
 
     virObjectUnlock(*obj);
-    virObjectUnref(*obj);
-    *obj = NULL;
+    g_clear_pointer(obj, virObjectUnref);
 }
 
 
diff --git a/src/conf/virsecretobj.c b/src/conf/virsecretobj.c
index a88f28fdda..ed62856d61 100644
--- a/src/conf/virsecretobj.c
+++ b/src/conf/virsecretobj.c
@@ -103,8 +103,7 @@ virSecretObjEndAPI(virSecretObj **obj)
         return;
 
     virObjectUnlock(*obj);
-    virObjectUnref(*obj);
-    *obj = NULL;
+    g_clear_pointer(obj, virObjectUnref);
 }
 
 
@@ -882,8 +881,7 @@ virSecretLoad(virSecretObjList *secrets,
 
     if (virSecretLoadValue(obj) < 0) {
         virSecretObjListRemove(secrets, obj);
-        virObjectUnref(obj);
-        obj = NULL;
+        g_clear_pointer(&obj, virObjectUnref);
     }
 
  cleanup:
diff --git a/src/conf/virstorageobj.c b/src/conf/virstorageobj.c
index 58c86e8b1b..d527132184 100644
--- a/src/conf/virstorageobj.c
+++ b/src/conf/virstorageobj.c
@@ -145,8 +145,7 @@ virStorageVolObjEndAPI(virStorageVolObj **obj)
         return;
 
     virObjectUnlock(*obj);
-    virObjectUnref(*obj);
-    *obj = NULL;
+    g_clear_pointer(obj, virObjectUnref);
 }
 
 
@@ -233,8 +232,7 @@ virStoragePoolObjEndAPI(virStoragePoolObj **obj)
         return;
 
     virObjectUnlock(*obj);
-    virObjectUnref(*obj);
-    *obj = NULL;
+    g_clear_pointer(obj, virObjectUnref);
 }
 
 
diff --git a/src/datatypes.c b/src/datatypes.c
index ad82703559..aa614612f9 100644
--- a/src/datatypes.c
+++ b/src/datatypes.c
@@ -187,8 +187,7 @@ virConnectCloseCallbackDataReset(virConnectCloseCallbackData *closeData)
 
     closeData->freeCallback = NULL;
     closeData->opaque = NULL;
-    virObjectUnref(closeData->conn);
-    closeData->conn = NULL;
+    g_clear_pointer(&closeData->conn, virObjectUnref);
 }
 
 /**
@@ -1141,8 +1140,7 @@ virAdmConnectCloseCallbackDataReset(virAdmConnectCloseCallbackData *cbdata)
     if (cbdata->freeCallback)
         cbdata->freeCallback(cbdata->opaque);
 
-    virObjectUnref(cbdata->conn);
-    cbdata->conn = NULL;
+    g_clear_pointer(&cbdata->conn, virObjectUnref);
     cbdata->freeCallback = NULL;
     cbdata->callback = NULL;
     cbdata->opaque = NULL;
diff --git a/src/hyperv/hyperv_wmi.c b/src/hyperv/hyperv_wmi.c
index 83d28b22aa..7dac5c46d1 100644
--- a/src/hyperv/hyperv_wmi.c
+++ b/src/hyperv/hyperv_wmi.c
@@ -925,8 +925,7 @@ hypervEnumAndPull(hypervPrivate *priv, hypervWqlQuery *wqlQuery,
 
     enumContext = wsmc_get_enum_context(response);
 
-    ws_xml_destroy_doc(response);
-    response = NULL;
+    g_clear_pointer(&response, ws_xml_destroy_doc);
 
     while (enumContext != NULL && *enumContext != '\0') {
         XML_TYPE_PTR data = NULL;
@@ -990,8 +989,7 @@ hypervEnumAndPull(hypervPrivate *priv, hypervWqlQuery *wqlQuery,
         VIR_FREE(enumContext);
         enumContext = wsmc_get_enum_context(response);
 
-        ws_xml_destroy_doc(response);
-        response = NULL;
+        g_clear_pointer(&response, ws_xml_destroy_doc);
     }
 
     *list = g_steal_pointer(&head);
diff --git a/src/interface/interface_backend_netcf.c b/src/interface/interface_backend_netcf.c
index 146a703953..92698ee769 100644
--- a/src/interface/interface_backend_netcf.c
+++ b/src/interface/interface_backend_netcf.c
@@ -136,8 +136,7 @@ netcfStateInitialize(bool privileged,
     return VIR_DRV_STATE_INIT_COMPLETE;
 
  error:
-    virObjectUnref(driver);
-    driver = NULL;
+    g_clear_pointer(&driver, virObjectUnref);
     return VIR_DRV_STATE_INIT_ERROR;
 }
 
@@ -148,8 +147,7 @@ netcfStateCleanup(void)
     if (!driver)
         return -1;
 
-    virObjectUnref(driver);
-    driver = NULL;
+    g_clear_pointer(&driver, virObjectUnref);
     return 0;
 }
 
@@ -685,8 +683,7 @@ netcfConnectListAllInterfaces(virConnectPtr conn,
             goto cleanup;
 
         if (!virConnectListAllInterfacesCheckACL(conn, def)) {
-            ncf_if_free(iface);
-            iface = NULL;
+            g_clear_pointer(&iface, ncf_if_free);
             continue;
         }
 
@@ -698,8 +695,7 @@ netcfConnectListAllInterfaces(virConnectPtr conn,
         }
         niface_objs++;
 
-        ncf_if_free(iface);
-        iface = NULL;
+        g_clear_pointer(&iface, ncf_if_free);
     }
 
     if (tmp_iface_objs) {
diff --git a/src/libvirt-domain.c b/src/libvirt-domain.c
index 5912551a49..f2d5f834ef 100644
--- a/src/libvirt-domain.c
+++ b/src/libvirt-domain.c
@@ -3205,8 +3205,7 @@ virDomainMigrateVersion3Full(virDomainPtr domain,
                 if (err &&
                     err->domain == VIR_FROM_QEMU &&
                     err->code != VIR_ERR_MIGRATE_FINISH_OK) {
-                    virFreeError(orig_err);
-                    orig_err = NULL;
+                    g_clear_pointer(&orig_err, virFreeError);
                 }
             }
         }
diff --git a/src/libxl/libxl_capabilities.c b/src/libxl/libxl_capabilities.c
index 970ad37306..6c7dfcb15e 100644
--- a/src/libxl/libxl_capabilities.c
+++ b/src/libxl/libxl_capabilities.c
@@ -305,8 +305,7 @@ libxlCapsInitNuma(libxl_ctx *ctx, virCaps *caps)
         for (i = 0; cpus && i < nr_nodes; i++)
             VIR_FREE(cpus[i]);
         if (caps->host.numa) {
-            virCapabilitiesHostNUMAUnref(caps->host.numa);
-            caps->host.numa = NULL;
+            g_clear_pointer(&caps->host.numa, virCapabilitiesHostNUMAUnref);
         }
         VIR_FREE(distances);
     }
diff --git a/src/libxl/libxl_migration.c b/src/libxl/libxl_migration.c
index bc2b5401da..6944c77eed 100644
--- a/src/libxl/libxl_migration.c
+++ b/src/libxl/libxl_migration.c
@@ -262,8 +262,7 @@ libxlDoMigrateDstReceive(void *opaque)
     for (i = 0; i < nsocks; i++) {
         virNetSocketRemoveIOCallback(socks[i]);
         virNetSocketClose(socks[i]);
-        virObjectUnref(socks[i]);
-        socks[i] = NULL;
+        g_clear_pointer(&socks[i], virObjectUnref);
     }
     args->nsocks = 0;
     VIR_FORCE_CLOSE(recvfd);
@@ -323,8 +322,7 @@ libxlMigrateDstReceive(virNetSocket *sock,
     for (i = 0; i < nsocks; i++) {
         virNetSocketUpdateIOCallback(socks[i], 0);
         virNetSocketRemoveIOCallback(socks[i]);
-        virNetSocketClose(socks[i]);
-        socks[i] = NULL;
+        g_clear_pointer(&socks[i], virNetSocketClose);
     }
     args->nsocks = 0;
     VIR_FORCE_CLOSE(recvfd);
diff --git a/src/locking/lock_daemon.c b/src/locking/lock_daemon.c
index 75ebd059d7..b44649bfbe 100644
--- a/src/locking/lock_daemon.c
+++ b/src/locking/lock_daemon.c
@@ -136,8 +136,7 @@ virLockDaemonNew(virLockDaemonConfig *config, bool privileged)
 
     if (virNetDaemonAddServer(lockd->dmn, srv) < 0)
         goto error;
-    virObjectUnref(srv);
-    srv = NULL;
+    g_clear_pointer(&srv, virObjectUnref);
 
     if (!(srv = virNetServerNew("admin", 1,
                                 0, 0, 0, config->admin_max_clients,
@@ -150,8 +149,7 @@ virLockDaemonNew(virLockDaemonConfig *config, bool privileged)
 
     if (virNetDaemonAddServer(lockd->dmn, srv) < 0)
          goto error;
-    virObjectUnref(srv);
-    srv = NULL;
+    g_clear_pointer(&srv, virObjectUnref);
 
     lockd->lockspaces = virHashNew(virLockDaemonLockSpaceDataFree);
 
diff --git a/src/locking/lock_driver_lockd.c b/src/locking/lock_driver_lockd.c
index 87afdbfb78..7e9081e9eb 100644
--- a/src/locking/lock_driver_lockd.c
+++ b/src/locking/lock_driver_lockd.c
@@ -372,8 +372,7 @@ static void virLockManagerLockDaemonFree(virLockManager *lock)
     if (!lock)
         return;
 
-    virLockManagerLockDaemonPrivateFree(lock->privateData);
-    lock->privateData = NULL;
+    g_clear_pointer(&lock->privateData, virLockManagerLockDaemonPrivateFree);
 }
 
 
diff --git a/src/logging/log_daemon.c b/src/logging/log_daemon.c
index 8c3eb66895..245df9dbbd 100644
--- a/src/logging/log_daemon.c
+++ b/src/logging/log_daemon.c
@@ -134,8 +134,7 @@ virLogDaemonNew(virLogDaemonConfig *config, bool privileged)
 
     if (virNetDaemonAddServer(logd->dmn, srv) < 0)
         goto error;
-    virObjectUnref(srv);
-    srv = NULL;
+    g_clear_pointer(&srv, virObjectUnref);
 
     if (!(srv = virNetServerNew("admin", 1,
                                 0, 0, 0, config->admin_max_clients,
@@ -148,8 +147,7 @@ virLogDaemonNew(virLogDaemonConfig *config, bool privileged)
 
     if (virNetDaemonAddServer(logd->dmn, srv) < 0)
         goto error;
-    virObjectUnref(srv);
-    srv = NULL;
+    g_clear_pointer(&srv, virObjectUnref);
 
     if (!(logd->handler = virLogHandlerNew(privileged,
                                            config->max_size,
diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c
index c4e3b66751..8cce06cd2b 100644
--- a/src/lxc/lxc_controller.c
+++ b/src/lxc/lxc_controller.c
@@ -220,8 +220,7 @@ static virLXCController *virLXCControllerNew(const char *name)
     return ctrl;
 
  error:
-    virLXCControllerFree(ctrl);
-    ctrl = NULL;
+    g_clear_pointer(&ctrl, virLXCControllerFree);
     goto cleanup;
 }
 
@@ -951,8 +950,7 @@ static int virLXCControllerSetupServer(virLXCController *ctrl)
 
     if (virNetServerAddService(srv, svc) < 0)
         goto error;
-    virObjectUnref(svc);
-    svc = NULL;
+    g_clear_pointer(&svc, virObjectUnref);
 
     if (!(ctrl->prog = virNetServerProgramNew(VIR_LXC_MONITOR_PROGRAM,
                                               VIR_LXC_MONITOR_PROGRAM_VERSION,
@@ -969,8 +967,7 @@ static int virLXCControllerSetupServer(virLXCController *ctrl)
 
  error:
     virObjectUnref(srv);
-    virObjectUnref(ctrl->daemon);
-    ctrl->daemon = NULL;
+    g_clear_pointer(&ctrl->daemon, virObjectUnref);
     virObjectUnref(svc);
     return -1;
 }
@@ -2067,8 +2064,7 @@ lxcCreateTty(virLXCController *ctrl, int *ttyprimary,
  cleanup:
     if (ret != 0) {
         VIR_FORCE_CLOSE(*ttyprimary);
-        g_free(*ttyName);
-        *ttyName = NULL;
+        g_clear_pointer(ttyName, g_free);
     }
 
     return ret;
diff --git a/src/lxc/lxc_domain.c b/src/lxc/lxc_domain.c
index 0920e91fd1..85795d1805 100644
--- a/src/lxc/lxc_domain.c
+++ b/src/lxc/lxc_domain.c
@@ -490,9 +490,7 @@ virLXCDomainSetRunlevel(virDomainObj *vm,
                                         lxcDomainInitctlCallback,
                                         &data);
  cleanup:
-    g_free(data.st);
-    data.st = NULL;
-    g_free(data.st_valid);
-    data.st_valid = NULL;
+    g_clear_pointer(&data.st, g_free);
+    g_clear_pointer(&data.st_valid, g_free);
     return ret;
 }
diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c
index 3d17b87e8c..ff83557bac 100644
--- a/src/lxc/lxc_driver.c
+++ b/src/lxc/lxc_driver.c
@@ -1472,8 +1472,7 @@ static int lxcStateInitialize(bool privileged,
     lxc_driver = g_new0(virLXCDriver, 1);
     lxc_driver->lockFD = -1;
     if (virMutexInit(&lxc_driver->lock) < 0) {
-        g_free(lxc_driver);
-        lxc_driver = NULL;
+        g_clear_pointer(&lxc_driver, g_free);
         return VIR_DRV_STATE_INIT_ERROR;
     }
 
@@ -1611,8 +1610,7 @@ static int lxcStateCleanup(void)
 
     virObjectUnref(lxc_driver->config);
     virMutexDestroy(&lxc_driver->lock);
-    g_free(lxc_driver);
-    lxc_driver = NULL;
+    g_clear_pointer(&lxc_driver, g_free);
 
     return 0;
 }
diff --git a/src/lxc/lxc_fuse.c b/src/lxc/lxc_fuse.c
index c2fe8f0e60..098f57d776 100644
--- a/src/lxc/lxc_fuse.c
+++ b/src/lxc/lxc_fuse.c
@@ -262,8 +262,7 @@ static void lxcFuseDestroy(struct virLXCFuse *fuse)
 {
     virMutexLock(&fuse->lock);
     fuse_unmount(fuse->mountpoint, fuse->ch);
-    fuse_destroy(fuse->fuse);
-    fuse->fuse = NULL;
+    g_clear_pointer(&fuse->fuse, fuse_destroy);
     virMutexUnlock(&fuse->lock);
 }
 
diff --git a/src/lxc/lxc_native.c b/src/lxc/lxc_native.c
index 84ed33521f..ea2f1effe1 100644
--- a/src/lxc/lxc_native.c
+++ b/src/lxc/lxc_native.c
@@ -518,8 +518,7 @@ lxcAddNetworkDefinition(virDomainDef *def, lxcNetworkParseData *data)
  error:
     for (i = 0; i < data->nips; i++)
         g_free(data->ips[i]);
-    g_free(data->ips);
-    data->ips = NULL;
+    g_clear_pointer(&data->ips, g_free);
     virDomainNetDefFree(net);
     virDomainHostdevDefFree(hostdev);
     return -1;
@@ -743,8 +742,7 @@ lxcConvertNetworkSettings(virDomainDef *def, virConf *properties)
  cleanup:
     for (i = 0; i < networks.ndata; i++)
         g_free(networks.parseData[i]);
-    g_free(networks.parseData);
-    networks.parseData = NULL;
+    g_clear_pointer(&networks.parseData, g_free);
     return ret;
 
  error:
@@ -752,8 +750,7 @@ lxcConvertNetworkSettings(virDomainDef *def, virConf *properties)
         lxcNetworkParseData *data = networks.parseData[i];
         for (j = 0; j < data->nips; j++)
             g_free(data->ips[j]);
-        g_free(data->ips);
-        data->ips = NULL;
+        g_clear_pointer(&data->ips, g_free);
     }
     goto cleanup;
 }
@@ -854,8 +851,7 @@ lxcSetMemTune(virDomainDef *def, virConf *properties)
         size = size / 1024;
         virDomainDefSetMemoryTotal(def, size);
         def->mem.hard_limit = virMemoryLimitTruncate(size);
-        g_free(value);
-        value = NULL;
+        g_clear_pointer(&value, g_free);
     }
 
     if (virConfGetValueString(properties,
@@ -864,8 +860,7 @@ lxcSetMemTune(virDomainDef *def, virConf *properties)
         if (lxcConvertSize(value, &size) < 0)
             return -1;
         def->mem.soft_limit = virMemoryLimitTruncate(size / 1024);
-        g_free(value);
-        value = NULL;
+        g_clear_pointer(&value, g_free);
     }
 
     if (virConfGetValueString(properties,
@@ -888,16 +883,14 @@ lxcSetCpuTune(virDomainDef *def, virConf *properties)
         if (virStrToLong_ull(value, NULL, 10, &def->cputune.shares) < 0)
             goto error;
         def->cputune.sharesSpecified = true;
-        g_free(value);
-        value = NULL;
+        g_clear_pointer(&value, g_free);
     }
 
     if (virConfGetValueString(properties, "lxc.cgroup.cpu.cfs_quota_us",
                               &value) > 0) {
         if (virStrToLong_ll(value, NULL, 10, &def->cputune.quota) < 0)
             goto error;
-        g_free(value);
-        value = NULL;
+        g_clear_pointer(&value, g_free);
     }
 
     if (virConfGetValueString(properties, "lxc.cgroup.cpu.cfs_period_us",
@@ -1111,8 +1104,7 @@ lxcParseConfigString(const char *config,
         else if (arch == VIR_ARCH_NONE && STREQ(value, "amd64"))
             arch = VIR_ARCH_X86_64;
         vmdef->os.arch = arch;
-        g_free(value);
-        value = NULL;
+        g_clear_pointer(&value, g_free);
     }
 
     vmdef->os.init = g_strdup("/sbin/init");
diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c
index db023630bd..0222e8a9b3 100644
--- a/src/lxc/lxc_process.c
+++ b/src/lxc/lxc_process.c
@@ -201,12 +201,9 @@ static void virLXCProcessCleanup(virLXCDriver *driver,
     /* Clear out dynamically assigned labels */
     if (vm->def->nseclabels &&
         vm->def->seclabels[0]->type == VIR_DOMAIN_SECLABEL_DYNAMIC) {
-        g_free(vm->def->seclabels[0]->model);
-        g_free(vm->def->seclabels[0]->label);
-        g_free(vm->def->seclabels[0]->imagelabel);
-        vm->def->seclabels[0]->model = NULL;
-        vm->def->seclabels[0]->label = NULL;
-        vm->def->seclabels[0]->imagelabel = NULL;
+        g_clear_pointer(&vm->def->seclabels[0]->model, g_free);
+        g_clear_pointer(&vm->def->seclabels[0]->label, g_free);
+        g_clear_pointer(&vm->def->seclabels[0]->imagelabel, g_free);
     }
 
     /* Stop autodestroy in case guest is restarted */
@@ -215,8 +212,7 @@ static void virLXCProcessCleanup(virLXCDriver *driver,
 
     if (priv->monitor) {
         virLXCMonitorClose(priv->monitor);
-        virObjectUnref(priv->monitor);
-        priv->monitor = NULL;
+        g_clear_pointer(&priv->monitor, virObjectUnref);
     }
 
     virPidFileDelete(cfg->stateDir, vm->def->name);
@@ -254,8 +250,7 @@ static void virLXCProcessCleanup(virLXCDriver *driver,
 
     if (priv->cgroup) {
         virCgroupRemove(priv->cgroup);
-        virCgroupFree(priv->cgroup);
-        priv->cgroup = NULL;
+        g_clear_pointer(&priv->cgroup, virCgroupFree);
     }
 
     /* Get machined to terminate the machine as it may not have cleaned it
@@ -263,8 +258,7 @@ static void virLXCProcessCleanup(virLXCDriver *driver,
      * the bug we are working around here.
      */
     virCgroupTerminateMachine(priv->machineName);
-    g_free(priv->machineName);
-    priv->machineName = NULL;
+    g_clear_pointer(&priv->machineName, g_free);
 
     /* The "release" hook cleans up additional resources */
     if (virHookPresent(VIR_HOOK_DRIVER_LXC)) {
@@ -680,8 +674,7 @@ virLXCProcessCleanInterfaces(virDomainDef *def)
     size_t i;
 
     for (i = 0; i < def->nnets; i++) {
-        g_free(def->nets[i]->ifname_guest_actual);
-        def->nets[i]->ifname_guest_actual = NULL;
+        g_clear_pointer(&def->nets[i]->ifname_guest_actual, g_free);
         VIR_DEBUG("Cleared net names: %s", def->nets[i]->ifname_guest);
     }
 }
diff --git a/src/network/bridge_driver_linux.c b/src/network/bridge_driver_linux.c
index 1c8be7103a..34982706be 100644
--- a/src/network/bridge_driver_linux.c
+++ b/src/network/bridge_driver_linux.c
@@ -50,10 +50,8 @@ static void networkSetupPrivateChains(void)
 
     VIR_DEBUG("Setting up global firewall chains");
 
-    virFreeError(errInitV4);
-    errInitV4 = NULL;
-    virFreeError(errInitV6);
-    errInitV6 = NULL;
+    g_clear_pointer(&errInitV4, virFreeError);
+    g_clear_pointer(&errInitV6, virFreeError);
 
     rc = iptablesSetupPrivateChains(VIR_FIREWALL_LAYER_IPV4);
     if (rc < 0) {
diff --git a/src/nwfilter/nwfilter_dhcpsnoop.c b/src/nwfilter/nwfilter_dhcpsnoop.c
index 386e7640c2..f8cffc7d57 100644
--- a/src/nwfilter/nwfilter_dhcpsnoop.c
+++ b/src/nwfilter/nwfilter_dhcpsnoop.c
@@ -1409,8 +1409,7 @@ virNWFilterDHCPSnoopThread(void *req0)
                 }
 
                 if (++errcount > PCAP_READ_MAXERRS) {
-                    pcap_close(pcapConf[i].handle);
-                    pcapConf[i].handle = NULL;
+                    g_clear_pointer(&pcapConf[i].handle, pcap_close);
 
                     /* protect req->binding->portdevname */
                     virNWFilterSnoopReqLock(req);
@@ -1542,8 +1541,7 @@ virNWFilterDHCPSnoopReq(virNWFilterTechDriver *techdriver,
             virNWFilterSnoopReqPut(req);
             return 0;
         }
-        virNWFilterBindingDefFree(req->binding);
-        req->binding = NULL;
+        g_clear_pointer(&req->binding, virNWFilterBindingDefFree);
     } else {
         req = virNWFilterSnoopReqNew(ifkey);
         if (!req)
diff --git a/src/nwfilter/nwfilter_driver.c b/src/nwfilter/nwfilter_driver.c
index 200451d6b1..08f138dd79 100644
--- a/src/nwfilter/nwfilter_driver.c
+++ b/src/nwfilter/nwfilter_driver.c
@@ -762,8 +762,7 @@ nwfilterBindingCreateXML(virConnectPtr conn,
 
     if (virNWFilterInstantiateFilter(driver, def) < 0) {
         virNWFilterBindingObjListRemove(driver->bindings, obj);
-        virObjectUnref(ret);
-        ret = NULL;
+        g_clear_pointer(&ret, virObjectUnref);
         goto cleanup;
     }
     virNWFilterBindingObjSave(obj, driver->bindingDir);
diff --git a/src/qemu/qemu_agent.c b/src/qemu/qemu_agent.c
index f33cd47078..4909fb336f 100644
--- a/src/qemu/qemu_agent.c
+++ b/src/qemu/qemu_agent.c
@@ -452,9 +452,8 @@ static void
 qemuAgentUnregister(qemuAgent *agent)
 {
     if (agent->watch) {
-        g_source_destroy(agent->watch);
         vir_g_source_unref(agent->watch, agent->context);
-        agent->watch = NULL;
+        g_clear_pointer(&agent->watch, g_source_destroy);
     }
 }
 
@@ -697,8 +696,7 @@ void qemuAgentClose(qemuAgent *agent)
 
     if (agent->socket) {
         qemuAgentUnregister(agent);
-        g_object_unref(agent->socket);
-        agent->socket = NULL;
+        g_clear_pointer(&agent->socket, g_object_unref);
         agent->fd = -1;
     }
 
diff --git a/src/qemu/qemu_backup.c b/src/qemu/qemu_backup.c
index 304a0d5a4f..1f7ab55eca 100644
--- a/src/qemu/qemu_backup.c
+++ b/src/qemu/qemu_backup.c
@@ -616,8 +616,7 @@ qemuBackupJobTerminate(virDomainObj *vm,
         qemuDomainEventEmitJobCompleted(priv->driver, vm);
     }
 
-    virDomainBackupDefFree(priv->backup);
-    priv->backup = NULL;
+    g_clear_pointer(&priv->backup, virDomainBackupDefFree);
 
     if (priv->job.asyncJob == QEMU_ASYNC_JOB_BACKUP)
         qemuDomainObjEndAsyncJob(priv->driver, vm);
diff --git a/src/qemu/qemu_blockjob.c b/src/qemu/qemu_blockjob.c
index 56cb83d999..726df95067 100644
--- a/src/qemu/qemu_blockjob.c
+++ b/src/qemu/qemu_blockjob.c
@@ -209,8 +209,7 @@ qemuBlockJobUnregister(qemuBlockJobData *job,
         diskPriv = QEMU_DOMAIN_DISK_PRIVATE(job->disk);
 
         if (job == diskPriv->blockjob) {
-            virObjectUnref(diskPriv->blockjob);
-            diskPriv->blockjob = NULL;
+            g_clear_pointer(&diskPriv->blockjob, virObjectUnref);
         }
 
         job->disk = NULL;
@@ -698,8 +697,7 @@ qemuBlockJobRewriteConfigDiskSource(virDomainObj *vm,
         /* discard any detected backing store */
         if (virStorageSourceIsBacking(n->backingStore) &&
             n->backingStore->detected) {
-            virObjectUnref(n->backingStore);
-            n->backingStore = NULL;
+            g_clear_pointer(&n->backingStore, virObjectUnref);
             break;
         }
     }
@@ -827,8 +825,7 @@ qemuBlockJobEventProcessLegacy(virQEMUDriver *driver,
              * Remove security driver metadata so that they are not leaked. */
             qemuBlockRemoveImageMetadata(driver, vm, disk->dst, disk->mirror);
 
-            virObjectUnref(disk->mirror);
-            disk->mirror = NULL;
+            g_clear_pointer(&disk->mirror, virObjectUnref);
         }
         disk->mirrorState = VIR_DOMAIN_DISK_MIRROR_STATE_NONE;
         disk->mirrorJob = VIR_DOMAIN_BLOCK_JOB_TYPE_UNKNOWN;
@@ -940,8 +937,7 @@ qemuBlockJobClearConfigChain(virDomainObj *vm,
     if (!virStorageSourceIsSameLocation(disk->src, cfgdisk->src))
         return;
 
-    virObjectUnref(cfgdisk->src->backingStore);
-    cfgdisk->src->backingStore = NULL;
+    g_clear_pointer(&cfgdisk->src->backingStore, virObjectUnref);
 }
 
 
@@ -1232,8 +1228,7 @@ qemuBlockJobProcessEventCompletedCommit(virQEMUDriver *driver,
     if (job->data.commit.deleteCommittedImages)
         qemuBlockJobDeleteImages(driver, vm, job->disk, job->data.commit.top);
 
-    virObjectUnref(job->data.commit.top);
-    job->data.commit.top = NULL;
+    g_clear_pointer(&job->data.commit.top, virObjectUnref);
 
     if (cfgbaseparent) {
         cfgbase = g_steal_pointer(&cfgbaseparent->backingStore);
@@ -1327,11 +1322,9 @@ qemuBlockJobProcessEventCompletedActiveCommit(virQEMUDriver *driver,
     if (job->data.commit.deleteCommittedImages)
         qemuBlockJobDeleteImages(driver, vm, job->disk, job->data.commit.top);
 
-    virObjectUnref(job->data.commit.top);
-    job->data.commit.top = NULL;
+    g_clear_pointer(&job->data.commit.top, virObjectUnref);
     /* the mirror element does not serve functional purpose for the commit job */
-    virObjectUnref(job->disk->mirror);
-    job->disk->mirror = NULL;
+    g_clear_pointer(&job->disk->mirror, virObjectUnref);
 }
 
 
@@ -1422,8 +1415,7 @@ qemuBlockJobProcessEventConcludedCopyAbort(virQEMUDriver *driver,
 
     /* activeWrite bitmap is removed automatically here */
     qemuBlockJobEventProcessConcludedRemoveChain(driver, vm, asyncJob, job->disk->mirror);
-    virObjectUnref(job->disk->mirror);
-    job->disk->mirror = NULL;
+    g_clear_pointer(&job->disk->mirror, virObjectUnref);
 }
 
 
@@ -1455,8 +1447,7 @@ qemuBlockJobProcessEventFailedActiveCommit(virQEMUDriver *driver,
      * not leaking security driver metadata is more important. */
     qemuBlockRemoveImageMetadata(driver, vm, disk->dst, disk->mirror);
 
-    virObjectUnref(disk->mirror);
-    disk->mirror = NULL;
+    g_clear_pointer(&disk->mirror, virObjectUnref);
 }
 
 
@@ -1472,8 +1463,7 @@ qemuBlockJobProcessEventConcludedCreate(virQEMUDriver *driver,
      * it will handle further hotplug of the created volume and also that
      * the 'chain' which was registered is under their control */
     if (job->synchronous) {
-        virObjectUnref(job->chain);
-        job->chain = NULL;
+        g_clear_pointer(&job->chain, virObjectUnref);
         return;
     }
 
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index 14b585c6e9..0c389a956f 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -124,8 +124,7 @@ qemuJobResetPrivate(void *opaque)
     priv->spiceMigration = false;
     priv->spiceMigrated = false;
     priv->dumpCompleted = false;
-    qemuMigrationParamsFree(priv->migParams);
-    priv->migParams = NULL;
+    g_clear_pointer(&priv->migParams, qemuMigrationParamsFree);
 }
 
 
@@ -1617,19 +1616,13 @@ qemuDomainObjStopWorker(virDomainObj *dom)
 void
 qemuDomainObjPrivateDataClear(qemuDomainObjPrivate *priv)
 {
-    g_strfreev(priv->qemuDevices);
-    priv->qemuDevices = NULL;
-
-    virCgroupFree(priv->cgroup);
-    priv->cgroup = NULL;
-
-    virPerfFree(priv->perf);
-    priv->perf = NULL;
+    g_clear_pointer(&priv->qemuDevices, g_strfreev);
+    g_clear_pointer(&priv->cgroup, virCgroupFree);
+    g_clear_pointer(&priv->perf, virPerfFree);
 
     VIR_FREE(priv->machineName);
 
-    virObjectUnref(priv->qemuCaps);
-    priv->qemuCaps = NULL;
+    g_clear_pointer(&priv->qemuCaps, virObjectUnref);
 
     VIR_FREE(priv->pidfile);
 
@@ -1639,41 +1632,25 @@ qemuDomainObjPrivateDataClear(qemuDomainObjPrivate *priv)
     priv->memPrealloc = false;
 
     /* remove automatic pinning data */
-    virBitmapFree(priv->autoNodeset);
-    priv->autoNodeset = NULL;
-    virBitmapFree(priv->autoCpuset);
-    priv->autoCpuset = NULL;
-
-    /* remove address data */
-    virDomainPCIAddressSetFree(priv->pciaddrs);
-    priv->pciaddrs = NULL;
-    virDomainUSBAddressSetFree(priv->usbaddrs);
-    priv->usbaddrs = NULL;
-
-    virCPUDefFree(priv->origCPU);
-    priv->origCPU = NULL;
-
-    /* clear previously used namespaces */
-    virBitmapFree(priv->namespaces);
-    priv->namespaces = NULL;
+    g_clear_pointer(&priv->autoNodeset, virBitmapFree);
+    g_clear_pointer(&priv->autoCpuset, virBitmapFree);
+    g_clear_pointer(&priv->pciaddrs, virDomainPCIAddressSetFree);
+    g_clear_pointer(&priv->usbaddrs, virDomainUSBAddressSetFree);
+    g_clear_pointer(&priv->origCPU, virCPUDefFree);
+    g_clear_pointer(&priv->namespaces, virBitmapFree);
 
     priv->rememberOwner = false;
 
     priv->reconnectBlockjobs = VIR_TRISTATE_BOOL_ABSENT;
     priv->allowReboot = VIR_TRISTATE_BOOL_ABSENT;
 
-    virBitmapFree(priv->migrationCaps);
-    priv->migrationCaps = NULL;
+    g_clear_pointer(&priv->migrationCaps, virBitmapFree);
 
     virHashRemoveAll(priv->blockjobs);
 
-    virObjectUnref(priv->pflash0);
-    priv->pflash0 = NULL;
-    virObjectUnref(priv->pflash1);
-    priv->pflash1 = NULL;
-
-    virDomainBackupDefFree(priv->backup);
-    priv->backup = NULL;
+    g_clear_pointer(&priv->pflash0, virObjectUnref);
+    g_clear_pointer(&priv->pflash1, virObjectUnref);
+    g_clear_pointer(&priv->backup, virDomainBackupDefFree);
 
     /* reset node name allocator */
     qemuDomainStorageIdReset(priv);
@@ -2961,8 +2938,7 @@ qemuDomainObjPrivateXMLParse(xmlXPathContextPtr ctxt,
 
     if (priv->namespaces &&
         virBitmapIsAllClear(priv->namespaces)) {
-        virBitmapFree(priv->namespaces);
-        priv->namespaces = NULL;
+        g_clear_pointer(&priv->namespaces, virBitmapFree);
     }
 
     priv->rememberOwner = virXPathBoolean("count(./rememberOwner) > 0", ctxt);
@@ -3099,12 +3075,9 @@ qemuDomainObjPrivateXMLParse(xmlXPathContextPtr ctxt,
     return 0;
 
  error:
-    virBitmapFree(priv->namespaces);
-    priv->namespaces = NULL;
-    virObjectUnref(priv->monConfig);
-    priv->monConfig = NULL;
-    g_strfreev(priv->qemuDevices);
-    priv->qemuDevices = NULL;
+    g_clear_pointer(&priv->namespaces, virBitmapFree);
+    g_clear_pointer(&priv->monConfig, virObjectUnref);
+    g_clear_pointer(&priv->qemuDevices, g_strfreev);
     return -1;
 }
 
@@ -3556,9 +3529,8 @@ qemuDomainDefClearDefaultAudioBackend(virQEMUDriver *driver,
 
     if (virDomainAudioIsEqual(def->audios[0], audio)) {
         virDomainAudioDefFree(def->audios[0]);
-        g_free(def->audios);
+        g_clear_pointer(&def->audios, g_free);
         def->naudios = 0;
-        def->audios = NULL;
     }
     virDomainAudioDefFree(audio);
 
diff --git a/src/qemu/qemu_domain_address.c b/src/qemu/qemu_domain_address.c
index 13aad4fc4d..4e7095d3a7 100644
--- a/src/qemu/qemu_domain_address.c
+++ b/src/qemu/qemu_domain_address.c
@@ -2738,8 +2738,7 @@ qemuDomainAssignPCIAddresses(virDomainDef *def,
         }
 
         nbuses = addrs->nbuses;
-        virDomainPCIAddressSetFree(addrs);
-        addrs = NULL;
+        g_clear_pointer(&addrs, virDomainPCIAddressSetFree);
     }
 
     if (!(addrs = qemuDomainPCIAddressSetCreate(def, qemuCaps, nbuses, false)))
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 698f57f00e..c01400acae 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -3921,8 +3921,7 @@ processSerialChangedEvent(virQEMUDriver *driver,
                 goto endjob;
         } else {
             if (priv->agent) {
-                qemuAgentClose(priv->agent);
-                priv->agent = NULL;
+                g_clear_pointer(&priv->agent, qemuAgentClose);
             }
             priv->agentError = false;
         }
@@ -7492,8 +7491,7 @@ qemuDomainDetachDeviceConfig(virDomainDef *vmdef,
                            _("domain has no watchdog"));
             return -1;
         }
-        virDomainWatchdogDefFree(vmdef->watchdog);
-        vmdef->watchdog = NULL;
+        g_clear_pointer(&vmdef->watchdog, virDomainWatchdogDefFree);
         break;
 
     case VIR_DOMAIN_DEVICE_INPUT:
@@ -7512,8 +7510,7 @@ qemuDomainDetachDeviceConfig(virDomainDef *vmdef,
                            _("matching vsock device not found"));
             return -1;
         }
-        virDomainVsockDefFree(vmdef->vsock);
-        vmdef->vsock = NULL;
+        g_clear_pointer(&vmdef->vsock, virDomainVsockDefFree);
         break;
 
     case VIR_DOMAIN_DEVICE_VIDEO:
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index c9548d7f35..ac2b07d940 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -645,8 +645,7 @@ qemuDomainChangeEjectableMedia(virQEMUDriver *driver,
     ignore_value(qemuDomainStorageSourceChainAccessRevoke(driver, vm, oldsrc));
 
     /* media was changed, so we can remove the old media definition now */
-    virObjectUnref(oldsrc);
-    oldsrc = NULL;
+    g_clear_pointer(&oldsrc, virObjectUnref);
 
     ret = 0;
 
@@ -4426,8 +4425,7 @@ qemuDomainRemoveDiskDevice(virQEMUDriver *driver,
         if (diskPriv->blockjob) {
             /* the block job keeps reference to the disk chain */
             diskPriv->blockjob->disk = NULL;
-            virObjectUnref(diskPriv->blockjob);
-            diskPriv->blockjob = NULL;
+            g_clear_pointer(&diskPriv->blockjob, virObjectUnref);
         } else {
             if (!(diskBackend = qemuBlockStorageSourceChainDetachPrepareBlockdev(disk->src)))
                 goto cleanup;
@@ -5013,8 +5011,7 @@ qemuDomainRemoveWatchdog(virDomainObj *vm,
               watchdog->info.alias, vm, vm->def->name);
 
     qemuDomainReleaseDeviceAddress(vm, &watchdog->info);
-    virDomainWatchdogDefFree(vm->def->watchdog);
-    vm->def->watchdog = NULL;
+    g_clear_pointer(&vm->def->watchdog, virDomainWatchdogDefFree);
     return 0;
 }
 
@@ -5056,8 +5053,7 @@ qemuDomainRemoveVsockDevice(virDomainObj *vm,
               dev->info.alias, vm, vm->def->name);
 
     qemuDomainReleaseDeviceAddress(vm, &dev->info);
-    virDomainVsockDefFree(vm->def->vsock);
-    vm->def->vsock = NULL;
+    g_clear_pointer(&vm->def->vsock, virDomainVsockDefFree);
     return 0;
 }
 
diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c
index 09b8c415f4..a1e6a8d1b1 100644
--- a/src/qemu/qemu_migration.c
+++ b/src/qemu/qemu_migration.c
@@ -845,8 +845,7 @@ qemuMigrationSrcNBDCopyCancel(virQEMUDriver *driver,
 
         qemuBlockStorageSourceDetachOneBlockdev(driver, vm, asyncJob,
                                                 diskPriv->migrSource);
-        virObjectUnref(diskPriv->migrSource);
-        diskPriv->migrSource = NULL;
+        g_clear_pointer(&diskPriv->migrSource, virObjectUnref);
     }
 
     ret = failed ? -1 : 0;
@@ -3659,8 +3658,7 @@ static void qemuMigrationSrcIOFunc(void *arg)
  abrt:
     virErrorPreserveLast(&err);
     if (err && err->code == VIR_ERR_OK) {
-        virFreeError(err);
-        err = NULL;
+        g_clear_pointer(&err, virFreeError);
     }
     virStreamAbort(data->st);
     virErrorRestore(&err);
@@ -4988,8 +4986,7 @@ qemuMigrationSrcPerformPeer2Peer3(virQEMUDriver *driver,
                 if (err &&
                     err->domain == VIR_FROM_QEMU &&
                     err->code != VIR_ERR_MIGRATE_FINISH_OK) {
-                    virFreeError(orig_err);
-                    orig_err = NULL;
+                    g_clear_pointer(&orig_err, virFreeError);
                 }
             }
         }
diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
index babf9e62fb..a6cabf7685 100644
--- a/src/qemu/qemu_monitor.c
+++ b/src/qemu/qemu_monitor.c
@@ -818,9 +818,8 @@ void
 qemuMonitorUnregister(qemuMonitor *mon)
 {
     if (mon->watch) {
-        g_source_destroy(mon->watch);
         vir_g_source_unref(mon->watch, mon->context);
-        mon->watch = NULL;
+        g_clear_pointer(&mon->watch, g_source_destroy);
     }
 }
 
@@ -837,8 +836,7 @@ qemuMonitorClose(qemuMonitor *mon)
 
     if (mon->socket) {
         qemuMonitorUnregister(mon);
-        g_object_unref(mon->socket);
-        mon->socket = NULL;
+        g_clear_pointer(&mon->socket, g_object_unref);
         mon->fd = -1;
     }
 
@@ -1557,8 +1555,7 @@ qemuMonitorCPUInfoClear(qemuMonitorCPUInfo *cpus,
         VIR_FREE(cpus[i].qom_path);
         VIR_FREE(cpus[i].alias);
         VIR_FREE(cpus[i].type);
-        virJSONValueFree(cpus[i].props);
-        cpus[i].props = NULL;
+        g_clear_pointer(&cpus[i].props, virJSONValueFree);
     }
 }
 
diff --git a/src/qemu/qemu_namespace.c b/src/qemu/qemu_namespace.c
index 94453033f5..3b41d72630 100644
--- a/src/qemu/qemu_namespace.c
+++ b/src/qemu/qemu_namespace.c
@@ -833,8 +833,7 @@ qemuDomainDisableNamespace(virDomainObj *vm,
     if (priv->namespaces) {
         ignore_value(virBitmapClearBit(priv->namespaces, ns));
         if (virBitmapIsAllClear(priv->namespaces)) {
-            virBitmapFree(priv->namespaces);
-            priv->namespaces = NULL;
+            g_clear_pointer(&priv->namespaces, virBitmapFree);
         }
     }
 }
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index ea586e54c1..53dcf76a74 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -8053,21 +8053,18 @@ void qemuProcessStop(virQEMUDriver *driver,
     priv->nbdPort = 0;
 
     if (priv->agent) {
-        qemuAgentClose(priv->agent);
-        priv->agent = NULL;
+        g_clear_pointer(&priv->agent, qemuAgentClose);
     }
     priv->agentError = false;
 
     if (priv->mon) {
-        qemuMonitorClose(priv->mon);
-        priv->mon = NULL;
+        g_clear_pointer(&priv->mon, qemuMonitorClose);
     }
 
     if (priv->monConfig) {
         if (priv->monConfig->type == VIR_DOMAIN_CHR_TYPE_UNIX)
             unlink(priv->monConfig->data.nix.path);
-        virObjectUnref(priv->monConfig);
-        priv->monConfig = NULL;
+        g_clear_pointer(&priv->monConfig, virObjectUnref);
     }
 
     qemuDomainObjStopWorker(vm);
@@ -8265,9 +8262,8 @@ void qemuProcessStop(virQEMUDriver *driver,
 
     for (i = 0; i < vm->ndeprecations; i++)
         g_free(vm->deprecations[i]);
-    g_free(vm->deprecations);
+    g_clear_pointer(&vm->deprecations, g_free);
     vm->ndeprecations = 0;
-    vm->deprecations = NULL;
     vm->taint = 0;
     vm->pid = -1;
     virDomainObjSetState(vm, VIR_DOMAIN_SHUTOFF, reason);
@@ -9078,14 +9074,12 @@ qemuProcessQMPStop(qemuProcessQMP *proc)
 {
     if (proc->mon) {
         virObjectUnlock(proc->mon);
-        qemuMonitorClose(proc->mon);
-        proc->mon = NULL;
+        g_clear_pointer(&proc->mon, qemuMonitorClose);
     }
 
     if (proc->cmd) {
         virCommandAbort(proc->cmd);
-        virCommandFree(proc->cmd);
-        proc->cmd = NULL;
+        g_clear_pointer(&proc->cmd, virCommandFree);
     }
 
     if (proc->monpath)
diff --git a/src/remote/remote_daemon_dispatch.c b/src/remote/remote_daemon_dispatch.c
index 689001889e..587cc0b3a5 100644
--- a/src/remote/remote_daemon_dispatch.c
+++ b/src/remote/remote_daemon_dispatch.c
@@ -1841,8 +1841,7 @@ remoteOpenConn(const char *uri,
 
  error:
     if (*conn) {
-        virConnectClose(*conn);
-        *conn = NULL;
+        g_clear_pointer(conn, virConnectClose);
     }
     goto cleanup;
 }
@@ -2191,8 +2190,7 @@ remoteDispatchConnectOpen(virNetServer *server G_GNUC_UNUSED,
     if (rv < 0) {
         virNetMessageSaveError(rerr);
         if (priv->conn) {
-            virObjectUnref(priv->conn);
-            priv->conn = NULL;
+            g_clear_pointer(&priv->conn, virObjectUnref);
         }
     }
     virMutexUnlock(&priv->lock);
@@ -3763,8 +3761,7 @@ remoteSASLFinish(virNetServer *server,
           "client=%p auth=%d identity=%s",
           client, REMOTE_AUTH_SASL, identity);
 
-    virObjectUnref(priv->sasl);
-    priv->sasl = NULL;
+    g_clear_pointer(&priv->sasl, virObjectUnref);
 
     return 0;
 }
@@ -3856,8 +3853,7 @@ remoteDispatchAuthSaslStart(virNetServer *server,
     goto error;
 
  error:
-    virObjectUnref(priv->sasl);
-    priv->sasl = NULL;
+    g_clear_pointer(&priv->sasl, virObjectUnref);
     virResetLastError();
     virReportError(VIR_ERR_AUTH_FAILED, "%s",
                    _("authentication failed"));
@@ -3951,8 +3947,7 @@ remoteDispatchAuthSaslStep(virNetServer *server,
     goto error;
 
  error:
-    virObjectUnref(priv->sasl);
-    priv->sasl = NULL;
+    g_clear_pointer(&priv->sasl, virObjectUnref);
     virResetLastError();
     virReportError(VIR_ERR_AUTH_FAILED, "%s",
                    _("authentication failed"));
diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c
index 5b7ccfaebd..d721da6d2d 100644
--- a/src/remote/remote_driver.c
+++ b/src/remote/remote_driver.c
@@ -1193,12 +1193,9 @@ doRemoteOpen(virConnectPtr conn,
     virObjectUnref(priv->lxcProgram);
     virObjectUnref(priv->qemuProgram);
     virNetClientClose(priv->client);
-    virObjectUnref(priv->client);
-    priv->client = NULL;
-    virObjectUnref(priv->closeCallback);
-    priv->closeCallback = NULL;
-    virObjectUnref(priv->tls);
-    priv->tls = NULL;
+    g_clear_pointer(&priv->client, virObjectUnref);
+    g_clear_pointer(&priv->closeCallback, virObjectUnref);
+    g_clear_pointer(&priv->tls, virObjectUnref);
 
     VIR_FREE(priv->hostname);
     return VIR_DRV_OPEN_ERROR;
@@ -1302,18 +1299,15 @@ doRemoteClose(virConnectPtr conn, struct private_data *priv)
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
         ret = -1;
 
-    virObjectUnref(priv->tls);
-    priv->tls = NULL;
+    g_clear_pointer(&priv->tls, virObjectUnref);
 
     virNetClientSetCloseCallback(priv->client,
                                  NULL,
                                  priv->closeCallback, virObjectFreeCallback);
 
     virNetClientClose(priv->client);
-    virObjectUnref(priv->client);
-    priv->client = NULL;
-    virObjectUnref(priv->closeCallback);
-    priv->closeCallback = NULL;
+    g_clear_pointer(&priv->client, virObjectUnref);
+    g_clear_pointer(&priv->closeCallback, virObjectUnref);
     virObjectUnref(priv->remoteProgram);
     virObjectUnref(priv->lxcProgram);
     virObjectUnref(priv->qemuProgram);
@@ -1325,8 +1319,7 @@ doRemoteClose(virConnectPtr conn, struct private_data *priv)
     /* See comment for remoteType. */
     VIR_FREE(priv->type);
 
-    virObjectUnref(priv->eventState);
-    priv->eventState = NULL;
+    g_clear_pointer(&priv->eventState, virObjectUnref);
 
     return ret;
 }
diff --git a/src/remote/remote_ssh_helper.c b/src/remote/remote_ssh_helper.c
index 78f02020a7..6403fe1761 100644
--- a/src/remote/remote_ssh_helper.c
+++ b/src/remote/remote_ssh_helper.c
@@ -63,8 +63,7 @@ virRemoteSSHHelperShutdown(virRemoteSSHHelper *proxy)
     if (proxy->sock) {
         virNetSocketRemoveIOCallback(proxy->sock);
         virNetSocketClose(proxy->sock);
-        virObjectUnref(proxy->sock);
-        proxy->sock = NULL;
+        g_clear_pointer(&proxy->sock, virObjectUnref);
     }
     VIR_FREE(proxy->sockToTerminal.data);
     VIR_FREE(proxy->terminalToSock.data);
diff --git a/src/rpc/virnetclient.c b/src/rpc/virnetclient.c
index f526ad89ec..2998551cd8 100644
--- a/src/rpc/virnetclient.c
+++ b/src/rpc/virnetclient.c
@@ -791,19 +791,15 @@ virNetClientCloseLocked(virNetClient *client)
     if (!client->sock)
         return;
 
-    virObjectUnref(client->sock);
-    client->sock = NULL;
-    virObjectUnref(client->tls);
-    client->tls = NULL;
+    g_clear_pointer(&client->sock, virObjectUnref);
+    g_clear_pointer(&client->tls, virObjectUnref);
 #if WITH_SASL
-    virObjectUnref(client->sasl);
-    client->sasl = NULL;
+    g_clear_pointer(&client->sasl, virObjectUnref);
 #endif
     ka = g_steal_pointer(&client->keepalive);
     client->wantClose = false;
 
-    virFreeError(client->error);
-    client->error = NULL;
+    g_clear_pointer(&client->error, virFreeError);
 
     if (ka || client->closeCb) {
         virNetClientCloseFunc closeCb = client->closeCb;
@@ -1025,8 +1021,7 @@ int virNetClientSetTLSSession(virNetClient *client,
     return 0;
 
  error:
-    virObjectUnref(client->tls);
-    client->tls = NULL;
+    g_clear_pointer(&client->tls, virObjectUnref);
     virObjectUnlock(client);
     return -1;
 }
diff --git a/src/rpc/virnetserverclient.c b/src/rpc/virnetserverclient.c
index 7050430e17..7d5c0965b8 100644
--- a/src/rpc/virnetserverclient.c
+++ b/src/rpc/virnetserverclient.c
@@ -1018,8 +1018,7 @@ virNetServerClientCloseLocked(virNetServerClient *client)
         virNetSocketRemoveIOCallback(client->sock);
 
     if (client->tls) {
-        virObjectUnref(client->tls);
-        client->tls = NULL;
+        g_clear_pointer(&client->tls, virObjectUnref);
     }
     client->wantClose = true;
 
@@ -1035,8 +1034,7 @@ virNetServerClientCloseLocked(virNetServerClient *client)
     }
 
     if (client->sock) {
-        virObjectUnref(client->sock);
-        client->sock = NULL;
+        g_clear_pointer(&client->sock, virObjectUnref);
     }
 }
 
@@ -1255,9 +1253,8 @@ static virNetMessage *virNetServerClientDispatchRead(virNetServerClient *client)
               msg->header.type, msg->header.status, msg->header.serial);
 
         if (virKeepAliveCheckMessage(client->keepalive, msg, &response)) {
-            virNetMessageFree(msg);
+            g_clear_pointer(&msg, virNetMessageFree);
             client->nrequests--;
-            msg = NULL;
 
             if (response &&
                 virNetServerClientSendMessageLocked(client, response) < 0)
@@ -1270,8 +1267,7 @@ static virNetMessage *virNetServerClientDispatchRead(virNetServerClient *client)
             while (filter) {
                 int ret = filter->func(client, msg, filter->opaque);
                 if (ret < 0) {
-                    virNetMessageFree(msg);
-                    msg = NULL;
+                    g_clear_pointer(&msg, virNetMessageFree);
                     client->wantClose = true;
                     break;
                 }
@@ -1375,8 +1371,7 @@ virNetServerClientDispatchWrite(virNetServerClient *client)
              */
             if (client->sasl) {
                 virNetSocketSetSASLSession(client->sock, client->sasl);
-                virObjectUnref(client->sasl);
-                client->sasl = NULL;
+                g_clear_pointer(&client->sasl, virObjectUnref);
             }
 #endif
 
diff --git a/src/rpc/virnettlscontext.c b/src/rpc/virnettlscontext.c
index 55da485f96..bdbf01855d 100644
--- a/src/rpc/virnettlscontext.c
+++ b/src/rpc/virnettlscontext.c
@@ -505,8 +505,7 @@ static gnutls_x509_crt_t virNetTLSContextLoadCertFromFile(const char *certFile,
 
  cleanup:
     if (ret != 0) {
-        gnutls_x509_crt_deinit(cert);
-        cert = NULL;
+        g_clear_pointer(&cert, gnutls_x509_crt_deinit);
     }
     VIR_FREE(buf);
     return cert;
diff --git a/src/secret/secret_driver.c b/src/secret/secret_driver.c
index de635bba3a..0220f394ef 100644
--- a/src/secret/secret_driver.c
+++ b/src/secret/secret_driver.c
@@ -275,8 +275,7 @@ secretDefineXML(virConnectPtr conn,
         def = g_steal_pointer(&objDef);
     } else {
         virSecretObjListRemove(driver->secrets, obj);
-        virObjectUnref(obj);
-        obj = NULL;
+        g_clear_pointer(&obj, virObjectUnref);
     }
 
  cleanup:
@@ -434,8 +433,7 @@ secretUndefine(virSecretPtr secret)
     virSecretObjDeleteData(obj);
 
     virSecretObjListRemove(driver->secrets, obj);
-    virObjectUnref(obj);
-    obj = NULL;
+    g_clear_pointer(&obj, virObjectUnref);
 
     ret = 0;
 
diff --git a/src/security/security_manager.c b/src/security/security_manager.c
index d8a03a19cb..f9c942de08 100644
--- a/src/security/security_manager.c
+++ b/src/security/security_manager.c
@@ -679,8 +679,7 @@ virSecurityManagerGenLabel(virSecurityManager *mgr,
 
         if (!sec_managers[i]->drv->domainGenSecurityLabel) {
             virReportUnsupportedError();
-            virSecurityLabelDefFree(seclabel);
-            seclabel = NULL;
+            g_clear_pointer(&seclabel, virSecurityLabelDefFree);
         } else {
             /* The seclabel must be added to @vm prior calling domainGenSecurityLabel
              * which may require seclabel to be presented already */
diff --git a/src/security/security_selinux.c b/src/security/security_selinux.c
index 0952431064..6f02baf2ce 100644
--- a/src/security/security_selinux.c
+++ b/src/security/security_selinux.c
@@ -687,8 +687,7 @@ virSecuritySELinuxLXCInitialize(virSecurityManager *mgr)
     return 0;
 
  error:
-    selabel_close(data->label_handle);
-    data->label_handle = NULL;
+    g_clear_pointer(&data->label_handle, selabel_close);
     VIR_FREE(data->domain_context);
     VIR_FREE(data->file_context);
     VIR_FREE(data->content_context);
@@ -758,8 +757,7 @@ virSecuritySELinuxQEMUInitialize(virSecurityManager *mgr)
     return 0;
 
  error:
-    selabel_close(data->label_handle);
-    data->label_handle = NULL;
+    g_clear_pointer(&data->label_handle, selabel_close);
     VIR_FREE(data->domain_context);
     VIR_FREE(data->alt_domain_context);
     VIR_FREE(data->file_context);
diff --git a/src/storage/storage_backend_iscsi_direct.c b/src/storage/storage_backend_iscsi_direct.c
index 37052ac4c6..302a338ec6 100644
--- a/src/storage/storage_backend_iscsi_direct.c
+++ b/src/storage/storage_backend_iscsi_direct.c
@@ -272,8 +272,7 @@ virISCSIDirectGetVolumeCapacity(struct iscsi_context *iscsi,
     if (inq->device_type == SCSI_INQUIRY_PERIPHERAL_DEVICE_TYPE_DIRECT_ACCESS) {
         struct scsi_readcapacity16 *rc16 = NULL;
 
-        scsi_free_scsi_task(task);
-        task = NULL;
+        g_clear_pointer(&task, scsi_free_scsi_task);
 
         if (!(task = iscsi_readcapacity16_sync(iscsi, lun)) ||
             task->status != SCSI_STATUS_GOOD) {
diff --git a/src/storage/storage_backend_rbd.c b/src/storage/storage_backend_rbd.c
index 8276ce20ab..2559991bc0 100644
--- a/src/storage/storage_backend_rbd.c
+++ b/src/storage/storage_backend_rbd.c
@@ -356,15 +356,13 @@ virStorageBackendRBDCloseRADOSConn(virStorageBackendRBDState *ptr)
 {
     if (ptr->ioctx != NULL) {
         VIR_DEBUG("Closing RADOS IoCTX");
-        rados_ioctx_destroy(ptr->ioctx);
+        g_clear_pointer(&ptr->ioctx, rados_ioctx_destroy);
     }
-    ptr->ioctx = NULL;
 
     if (ptr->cluster != NULL) {
         VIR_DEBUG("Closing RADOS connection");
-        rados_shutdown(ptr->cluster);
+        g_clear_pointer(&ptr->cluster, rados_shutdown);
     }
-    ptr->cluster = NULL;
 
     VIR_DEBUG("RADOS connection existed for %ld seconds",
               time(0) - ptr->starttime);
diff --git a/src/storage/storage_util.c b/src/storage/storage_util.c
index f88d225e96..f543dc9f3d 100644
--- a/src/storage/storage_util.c
+++ b/src/storage/storage_util.c
@@ -3593,8 +3593,7 @@ virStorageBackendRefreshLocal(virStoragePoolObj *pool)
             if (err == -2) {
                 /* Silently ignore non-regular files,
                  * eg 'lost+found', dangling symbolic link */
-                virStorageVolDefFree(vol);
-                vol = NULL;
+                g_clear_pointer(&vol, virStorageVolDefFree);
                 continue;
             }
             return -1;
diff --git a/src/storage_file/storage_file_probe.c b/src/storage_file/storage_file_probe.c
index effd9aaa7d..54e095ffd3 100644
--- a/src/storage_file/storage_file_probe.c
+++ b/src/storage_file/storage_file_probe.c
@@ -969,8 +969,7 @@ virStorageFileProbeGetMetadata(virStorageSource *meta,
             return -1;
     }
 
-    virBitmapFree(meta->features);
-    meta->features = NULL;
+    g_clear_pointer(&meta->features, virBitmapFree);
     if (fileTypeInfo[meta->format].getFeatures != NULL &&
         fileTypeInfo[meta->format].getFeatures(&meta->features, meta->format, buf, len) < 0)
         return -1;
diff --git a/src/storage_file/storage_source.c b/src/storage_file/storage_source.c
index c2bdc39f25..698b9eb79d 100644
--- a/src/storage_file/storage_source.c
+++ b/src/storage_file/storage_source.c
@@ -420,8 +420,7 @@ virStorageSourceNewFromBackingAbsolute(const char *path,
          * also used in other places. For backing store detection the
          * authentication data would be invalid anyways, so we clear it */
         if (def->auth) {
-            virStorageAuthDefFree(def->auth);
-            def->auth = NULL;
+            g_clear_pointer(&def->auth, virStorageAuthDefFree);
         }
     }
 
diff --git a/src/test/test_driver.c b/src/test/test_driver.c
index f900123941..03c41ca192 100644
--- a/src/test/test_driver.c
+++ b/src/test/test_driver.c
@@ -1537,8 +1537,7 @@ testConnectOpen(virConnectPtr conn,
 
     /* Fake authentication. */
     if (testConnectAuthenticate(conn, auth) < 0) {
-        testDriverCloseInternal(conn->privateData);
-        conn->privateData = NULL;
+        g_clear_pointer(&conn->privateData, testDriverCloseInternal);
         return VIR_DRV_OPEN_ERROR;
     }
 
@@ -1549,8 +1548,7 @@ testConnectOpen(virConnectPtr conn,
 static int
 testConnectClose(virConnectPtr conn)
 {
-    testDriverCloseInternal(conn->privateData);
-    conn->privateData = NULL;
+    g_clear_pointer(&conn->privateData, testDriverCloseInternal);
     return 0;
 }
 
diff --git a/src/util/viralloc.c b/src/util/viralloc.c
index 17ce5f3dbe..4ebbf1cf8c 100644
--- a/src/util/viralloc.c
+++ b/src/util/viralloc.c
@@ -136,8 +136,7 @@ void virShrinkN(void *ptrptr, size_t size, size_t *countptr, size_t toremove)
     if (toremove < *countptr) {
         virReallocN(ptrptr, size, *countptr -= toremove);
     } else {
-        g_free(*((void **)ptrptr));
-        *((void **)ptrptr) = NULL;
+        g_clear_pointer(((void **)ptrptr), g_free);
         *countptr = 0;
     }
 }
diff --git a/src/util/virconf.c b/src/util/virconf.c
index cd45c5f657..0d0c04a824 100644
--- a/src/util/virconf.c
+++ b/src/util/virconf.c
@@ -919,8 +919,7 @@ int virConfGetValueStringList(virConf *conf,
     if (!cval)
         return 0;
 
-    g_strfreev(*values);
-    *values = NULL;
+    g_clear_pointer(values, g_strfreev);
 
     switch (cval->type) {
     case VIR_CONF_LIST:
diff --git a/src/util/virerror.c b/src/util/virerror.c
index d9e2c65dc8..e864a50fba 100644
--- a/src/util/virerror.c
+++ b/src/util/virerror.c
@@ -445,8 +445,7 @@ virErrorRestore(virErrorPtr *savederr)
         return;
 
     virSetError(*savederr);
-    virFreeError(*savederr);
-    *savederr = NULL;
+    g_clear_pointer(savederr, virFreeError);
     errno = saved_errno;
 }
 
diff --git a/src/util/vireventglib.c b/src/util/vireventglib.c
index 983787932f..fc04d8f712 100644
--- a/src/util/vireventglib.c
+++ b/src/util/vireventglib.c
@@ -228,8 +228,7 @@ virEventGLibHandleUpdate(int watch,
 
         VIR_DEBUG("Removed old handle source=%p", data->source);
         g_source_destroy(data->source);
-        vir_g_source_unref(data->source, NULL);
-        data->source = NULL;
+        g_clear_pointer(&data->source, g_source_destroy);
         data->events = 0;
     }
 
@@ -276,9 +275,8 @@ virEventGLibHandleRemove(int watch)
               data, watch, data->fd);
 
     if (data->source != NULL) {
-        g_source_destroy(data->source);
         vir_g_source_unref(data->source, NULL);
-        data->source = NULL;
+        g_clear_pointer(&data->source, g_source_destroy);
         data->events = 0;
     }
 
@@ -419,9 +417,8 @@ virEventGLibTimeoutUpdate(int timer,
         if (data->source == NULL)
             goto cleanup;
 
-        g_source_destroy(data->source);
         vir_g_source_unref(data->source, NULL);
-        data->source = NULL;
+        g_clear_pointer(&data->source, g_source_destroy);
     }
 
  cleanup:
@@ -468,9 +465,8 @@ virEventGLibTimeoutRemove(int timer)
               data, timer);
 
     if (data->source != NULL) {
-        g_source_destroy(data->source);
         vir_g_source_unref(data->source, NULL);
-        data->source = NULL;
+        g_clear_pointer(&data->source, g_source_destroy);
     }
 
     /* since the actual timeout deletion is done asynchronously, a timeoutUpdate call may
diff --git a/src/util/virfile.c b/src/util/virfile.c
index 2d9c89ebbe..0b79772da7 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -3723,8 +3723,7 @@ virFileSetACLs(const char *file,
 void
 virFileFreeACLs(void **acl)
 {
-    acl_free(*acl);
-    *acl = NULL;
+    g_clear_pointer(acl, acl_free);
 }
 
 #else /* !defined(WITH_LIBACL) */
diff --git a/src/util/virfilecache.c b/src/util/virfilecache.c
index c140f9d5ea..cfcfc26e19 100644
--- a/src/util/virfilecache.c
+++ b/src/util/virfilecache.c
@@ -209,8 +209,7 @@ virFileCacheNewData(virFileCache *cache,
             return NULL;
 
         if (virFileCacheSave(cache, name, data) < 0) {
-            virObjectUnref(data);
-            data = NULL;
+            g_clear_pointer(&data, virObjectUnref);
         }
     }
 
@@ -273,8 +272,7 @@ virFileCacheValidate(virFileCache *cache,
         if (*data) {
             VIR_DEBUG("Caching data '%p' for '%s'", *data, name);
             if (virHashAddEntry(cache->table, name, *data) < 0) {
-                virObjectUnref(*data);
-                *data = NULL;
+                g_clear_pointer(data, virObjectUnref);
             }
         }
     }
diff --git a/src/util/virgdbus.c b/src/util/virgdbus.c
index 4ad1a5c842..87b48256af 100644
--- a/src/util/virgdbus.c
+++ b/src/util/virgdbus.c
@@ -174,8 +174,7 @@ virGDBusCloseSystemBus(void)
 
     g_dbus_connection_flush_sync(systemBus, NULL, NULL);
     g_dbus_connection_close_sync(systemBus, NULL, NULL);
-    g_object_unref(systemBus);
-    systemBus = NULL;
+    g_clear_pointer(&systemBus, g_object_unref);
 }
 
 
diff --git a/src/util/virmdev.c b/src/util/virmdev.c
index d12c7b87a0..eb566982b1 100644
--- a/src/util/virmdev.c
+++ b/src/util/virmdev.c
@@ -282,8 +282,7 @@ virMediatedDeviceListDispose(void *obj)
     size_t i;
 
     for (i = 0; i < list->count; i++) {
-        virMediatedDeviceFree(list->devs[i]);
-        list->devs[i] = NULL;
+        g_clear_pointer(&list->devs[i], virMediatedDeviceFree);
     }
 
     list->count = 0;
diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c
index cbe6ebee8f..fcf679ec37 100644
--- a/src/util/virnetdev.c
+++ b/src/util/virnetdev.c
@@ -2948,8 +2948,7 @@ int virNetDevGetRxFilter(const char *ifname,
     ret = 0;
  cleanup:
     if (ret < 0) {
-        virNetDevRxFilterFree(fil);
-        fil = NULL;
+        g_clear_pointer(&fil, virNetDevRxFilterFree);
     }
 
     *filter = fil;
diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c
index 81c49abd5d..3216765492 100644
--- a/src/util/virnetlink.c
+++ b/src/util/virnetlink.c
@@ -192,8 +192,7 @@ void
 virNetlinkShutdown(void)
 {
     if (placeholder_nlhandle) {
-        virNetlinkFree(placeholder_nlhandle);
-        placeholder_nlhandle = NULL;
+        g_clear_pointer(&placeholder_nlhandle, virNetlinkFree);
     }
 }
 
diff --git a/src/util/virpci.c b/src/util/virpci.c
index 0d476cd8b4..adc255f438 100644
--- a/src/util/virpci.c
+++ b/src/util/virpci.c
@@ -1666,8 +1666,7 @@ virPCIDeviceListDispose(void *obj)
     size_t i;
 
     for (i = 0; i < list->count; i++) {
-        virPCIDeviceFree(list->devs[i]);
-        list->devs[i] = NULL;
+        g_clear_pointer(&list->devs[i], virPCIDeviceFree);
     }
 
     list->count = 0;
diff --git a/src/util/virresctrl.c b/src/util/virresctrl.c
index edbf078654..c46a78f113 100644
--- a/src/util/virresctrl.c
+++ b/src/util/virresctrl.c
@@ -1705,8 +1705,7 @@ virResctrlAllocGetGroup(virResctrlInfo *resctrl,
 
  error:
     VIR_FREE(schemata);
-    virObjectUnref(*alloc);
-    *alloc = NULL;
+    g_clear_pointer(alloc, virObjectUnref);
     return -1;
 }
 
diff --git a/src/util/virstring.c b/src/util/virstring.c
index ad0b158ad4..7b7aee6179 100644
--- a/src/util/virstring.c
+++ b/src/util/virstring.c
@@ -638,8 +638,7 @@ virStringSearch(const char *str,
 
  cleanup:
     if (ret < 0) {
-        g_strfreev(*matches);
-        *matches = NULL;
+        g_clear_pointer(matches, g_strfreev);
     }
     return ret;
 }
diff --git a/src/util/virsysinfo.c b/src/util/virsysinfo.c
index af9e03c5ac..376d5d4816 100644
--- a/src/util/virsysinfo.c
+++ b/src/util/virsysinfo.c
@@ -254,8 +254,7 @@ virSysinfoParsePPCSystem(const char *base, virSysinfoSystemDef **sysdef)
 
     if (!def->manufacturer && !def->product && !def->version &&
         !def->serial && !def->uuid && !def->sku && !def->family) {
-        virSysinfoSystemDefFree(def);
-        def = NULL;
+        g_clear_pointer(&def, virSysinfoSystemDefFree);
     }
 
     *sysdef = g_steal_pointer(&def);
@@ -375,8 +374,7 @@ virSysinfoParseARMSystem(const char *base, virSysinfoSystemDef **sysdef)
 
     if (!def->manufacturer && !def->product && !def->version &&
         !def->serial && !def->uuid && !def->sku && !def->family) {
-        virSysinfoSystemDefFree(def);
-        def = NULL;
+        g_clear_pointer(&def, virSysinfoSystemDefFree);
     }
 
     *sysdef = g_steal_pointer(&def);
@@ -509,8 +507,7 @@ virSysinfoParseS390System(const char *base, virSysinfoSystemDef **sysdef)
 
     if (!def->manufacturer && !def->product && !def->version &&
         !def->serial && !def->uuid && !def->sku && !def->family) {
-        virSysinfoSystemDefFree(def);
-        def = NULL;
+        g_clear_pointer(&def, virSysinfoSystemDefFree);
     }
 
     *sysdef = g_steal_pointer(&def);
@@ -672,8 +669,7 @@ virSysinfoParseBIOS(const char *base, virSysinfoBIOSDef **bios)
 
     if (!def->vendor && !def->version &&
         !def->date && !def->release) {
-        virSysinfoBIOSDefFree(def);
-        def = NULL;
+        g_clear_pointer(&def, virSysinfoBIOSDefFree);
     }
 
     *bios = g_steal_pointer(&def);
@@ -748,8 +744,7 @@ virSysinfoParseX86System(const char *base, virSysinfoSystemDef **sysdef)
 
     if (!def->manufacturer && !def->product && !def->version &&
         !def->serial && !def->uuid && !def->sku && !def->family) {
-        virSysinfoSystemDefFree(def);
-        def = NULL;
+        g_clear_pointer(&def, virSysinfoSystemDefFree);
     }
 
     *sysdef = g_steal_pointer(&def);
@@ -889,8 +884,7 @@ virSysinfoParseX86Chassis(const char *base,
 
     if (!def->manufacturer && !def->version &&
         !def->serial && !def->asset && !def->sku) {
-        virSysinfoChassisDefFree(def);
-        def = NULL;
+        g_clear_pointer(&def, virSysinfoChassisDefFree);
     }
 
     *chassisdef = g_steal_pointer(&def);
diff --git a/src/util/virtpm.c b/src/util/virtpm.c
index cf4c7c458f..c02b42f948 100644
--- a/src/util/virtpm.c
+++ b/src/util/virtpm.c
@@ -330,8 +330,7 @@ virTPMEmulatorInit(bool quiet)
                 return -1;
             }
             swtpmBinaries[i].path = g_steal_pointer(&path);
-            virBitmapFree(swtpmBinaries[i].caps);
-            swtpmBinaries[i].caps = NULL;
+            g_clear_pointer(&swtpmBinaries[i].caps, virBitmapFree);
         }
     }
 
diff --git a/src/vbox/vbox_XPCOMCGlue.c b/src/vbox/vbox_XPCOMCGlue.c
index 2936ff0edb..d7b4ae9aad 100644
--- a/src/vbox/vbox_XPCOMCGlue.c
+++ b/src/vbox/vbox_XPCOMCGlue.c
@@ -152,8 +152,7 @@ tryLoadOne(const char *dir, bool setAppHome, bool ignoreMissing,
 
  cleanup:
     if (hVBoxXPCOMC != NULL && result < 0) {
-        dlclose(hVBoxXPCOMC);
-        hVBoxXPCOMC = NULL;
+        g_clear_pointer(&hVBoxXPCOMC, dlclose);
     }
 
     VIR_FREE(name);
diff --git a/src/vbox/vbox_common.c b/src/vbox/vbox_common.c
index db54eacb3b..40180b0dfd 100644
--- a/src/vbox/vbox_common.c
+++ b/src/vbox/vbox_common.c
@@ -4953,8 +4953,7 @@ vboxSnapshotRedefine(virDomainPtr dom,
         tmp = virStringReplace(newSnapshotPtr->storageController,
                                searchResultTab[it],
                                uuidReplacing);
-        g_strfreev(searchResultTab);
-        searchResultTab = NULL;
+        g_clear_pointer(&searchResultTab, g_strfreev);
         VIR_FREE(newSnapshotPtr->storageController);
         if (!tmp)
             goto cleanup;
diff --git a/src/vbox/vbox_snapshot_conf.c b/src/vbox/vbox_snapshot_conf.c
index 5894ee1cac..6b47893b5e 100644
--- a/src/vbox/vbox_snapshot_conf.c
+++ b/src/vbox/vbox_snapshot_conf.c
@@ -104,8 +104,7 @@ virVBoxSnapshotConfCreateVBoxSnapshotConfHardDiskPtr(xmlNodePtr diskNode,
     VIR_FREE(location);
     VIR_FREE(tmp);
     if (result < 0) {
-        virVboxSnapshotConfHardDiskFree(hardDisk);
-        hardDisk = NULL;
+        g_clear_pointer(&hardDisk, virVboxSnapshotConfHardDiskFree);
     }
     return hardDisk;
 }
@@ -165,8 +164,7 @@ virVBoxSnapshotConfRetrieveMediaRegistry(xmlNodePtr mediaRegistryNode,
 
  cleanup:
     if (result < 0) {
-        virVBoxSnapshotConfMediaRegistryFree(mediaRegistry);
-        mediaRegistry = NULL;
+        g_clear_pointer(&mediaRegistry, virVBoxSnapshotConfMediaRegistryFree);
     }
     VIR_FREE(nodes);
     return mediaRegistry;
@@ -264,8 +262,7 @@ virVBoxSnapshotConfRetrieveSnapshot(xmlNodePtr snapshotNode,
 
  cleanup:
     if (result < 0) {
-        virVBoxSnapshotConfSnapshotFree(snapshot);
-        snapshot = NULL;
+        g_clear_pointer(&snapshot, virVBoxSnapshotConfSnapshotFree);
     }
     VIR_FREE(nodes);
     VIR_FREE(uuid);
@@ -348,8 +345,7 @@ virVBoxSnapshotConfCreateHardDiskNode(virVBoxSnapshotConfHardDisk *hardDisk)
  cleanup:
     if (result < 0) {
         xmlUnlinkNode(ret);
-        xmlFreeNode(ret);
-        ret = NULL;
+        g_clear_pointer(&ret, xmlFreeNode);
     }
     VIR_FREE(uuid);
     return ret;
diff --git a/src/vmx/vmx.c b/src/vmx/vmx.c
index ca65caa9a1..8150bbfaa9 100644
--- a/src/vmx/vmx.c
+++ b/src/vmx/vmx.c
@@ -1425,8 +1425,7 @@ virVMXParseConfig(virVMXContext *ctx,
     if (encoding == NULL || STRCASEEQ(encoding, "UTF-8")) {
         /* nothing */
     } else {
-        virConfFree(conf);
-        conf = NULL;
+        g_clear_pointer(&conf, virConfFree);
 
         utf8 = virVMXConvertToUTF8(encoding, vmx);
 
@@ -2053,8 +2052,7 @@ virVMXParseVNC(virConf *conf, virDomainGraphicsDef **def)
 
  failure:
     VIR_FREE(listenAddr);
-    virDomainGraphicsDefFree(*def);
-    *def = NULL;
+    g_clear_pointer(def, virDomainGraphicsDefFree);
 
     return -1;
 }
@@ -2556,8 +2554,7 @@ virVMXParseDisk(virVMXContext *ctx, virDomainXMLOption *xmlopt, virConf *conf,
 
  cleanup:
     if (result < 0) {
-        virDomainDiskDefFree(*def);
-        *def = NULL;
+        g_clear_pointer(def, virDomainDiskDefFree);
     }
 
     VIR_FREE(prefix);
@@ -2569,8 +2566,7 @@ virVMXParseDisk(virVMXContext *ctx, virDomainXMLOption *xmlopt, virConf *conf,
     return result;
 
  ignore:
-    virDomainDiskDefFree(*def);
-    *def = NULL;
+    g_clear_pointer(def, virDomainDiskDefFree);
 
     result = 0;
 
@@ -2653,8 +2649,7 @@ virVMXParseFileSystem(virConf *conf, int number, virDomainFSDef **def)
 
  cleanup:
     if (result < 0) {
-        virDomainFSDefFree(*def);
-        *def = NULL;
+        g_clear_pointer(def, virDomainFSDefFree);
     }
 
     VIR_FREE(hostPath);
@@ -2869,8 +2864,7 @@ virVMXParseEthernet(virConf *conf, int controller, virDomainNetDef **def)
 
  cleanup:
     if (result < 0) {
-        virDomainNetDefFree(*def);
-        *def = NULL;
+        g_clear_pointer(def, virDomainNetDefFree);
     }
 
     VIR_FREE(networkName);
@@ -3051,8 +3045,7 @@ virVMXParseSerial(virVMXContext *ctx, virConf *conf, int port,
 
  cleanup:
     if (result < 0) {
-        virDomainChrDefFree(*def);
-        *def = NULL;
+        g_clear_pointer(def, virDomainChrDefFree);
     }
 
     VIR_FREE(fileType);
@@ -3153,8 +3146,7 @@ virVMXParseParallel(virVMXContext *ctx, virConf *conf, int port,
 
  cleanup:
     if (result < 0) {
-        virDomainChrDefFree(*def);
-        *def = NULL;
+        g_clear_pointer(def, virDomainChrDefFree);
     }
 
     VIR_FREE(fileType);
@@ -3191,8 +3183,7 @@ virVMXParseSVGA(virConf *conf, virDomainVideoDef **def)
 
  cleanup:
     if (result < 0) {
-        virDomainVideoDefFree(*def);
-        *def = NULL;
+        g_clear_pointer(def, virDomainVideoDefFree);
     }
 
     return result;
diff --git a/src/vz/vz_driver.c b/src/vz/vz_driver.c
index c16cc2be00..be3e5e4b49 100644
--- a/src/vz/vz_driver.c
+++ b/src/vz/vz_driver.c
@@ -4071,8 +4071,7 @@ static int
 vzStateCleanup(void)
 {
     if (vz_driver_privileged) {
-        virObjectUnref(vz_driver);
-        vz_driver = NULL;
+        g_clear_pointer(&vz_driver, virObjectUnref);
         if (vz_driver_lock_fd != -1)
             virPidFileRelease(VZ_STATEDIR, "driver", vz_driver_lock_fd);
         virMutexDestroy(&vz_driver_lock);
diff --git a/src/vz/vz_sdk.c b/src/vz/vz_sdk.c
index ccfd3e9d55..f149360c03 100644
--- a/src/vz/vz_sdk.c
+++ b/src/vz/vz_sdk.c
@@ -179,8 +179,7 @@ getJobResultHelper(PRL_HANDLE job, unsigned int timeout, PRL_HANDLE *result,
         ret = PrlJob_GetResult(job, result);
         if (PRL_FAILED(ret)) {
             logPrlErrorHelper(ret, filename, funcname, linenr);
-            PrlHandle_Free(*result);
-            *result = NULL;
+            g_clear_pointer(result, PrlHandle_Free);
             goto cleanup;
         }
 
diff --git a/tests/commandtest.c b/tests/commandtest.c
index 5267e1bf1c..74e60a072b 100644
--- a/tests/commandtest.c
+++ b/tests/commandtest.c
@@ -446,8 +446,7 @@ static int test13(const void *unused G_GNUC_UNUSED)
     if (!outactual)
         goto cleanup;
 
-    virCommandFree(cmd);
-    cmd = NULL;
+    g_clear_pointer(&cmd, virCommandFree);
 
     if (STRNEQ(outactual, outexpect)) {
         virTestDifference(stderr, outexpect, outactual);
@@ -668,8 +667,7 @@ static int test18(const void *unused G_GNUC_UNUSED)
         goto cleanup;
     }
 
-    virCommandFree(cmd);
-    cmd = NULL;
+    g_clear_pointer(&cmd, virCommandFree);
     if (kill(pid, 0) != 0) {
         printf("daemon should still be running\n");
         goto cleanup;
diff --git a/tests/cputest.c b/tests/cputest.c
index 0f0621292a..609b5df7c6 100644
--- a/tests/cputest.c
+++ b/tests/cputest.c
@@ -293,8 +293,7 @@ cpuTestBaseline(const void *arg)
     if (baseline &&
         (data->flags & VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES) &&
         virCPUExpandFeatures(data->arch, baseline) < 0) {
-        virCPUDefFree(baseline);
-        baseline = NULL;
+        g_clear_pointer(&baseline, virCPUDefFree);
     }
 
     if (data->result < 0) {
diff --git a/tests/qemumonitortestutils.c b/tests/qemumonitortestutils.c
index 073ae5d73a..ce8e6e1645 100644
--- a/tests/qemumonitortestutils.c
+++ b/tests/qemumonitortestutils.c
@@ -327,8 +327,7 @@ qemuMonitorTestIO(virNetSocket *sock,
     if (err) {
         virNetSocketRemoveIOCallback(sock);
         virNetSocketClose(sock);
-        virObjectUnref(test->client);
-        test->client = NULL;
+        g_clear_pointer(&test->client, virObjectUnref);
     } else {
         events = VIR_EVENT_HANDLE_READABLE;
 
diff --git a/tests/virnetdaemontest.c b/tests/virnetdaemontest.c
index 24f4761bb7..2a8bc0ec93 100644
--- a/tests/virnetdaemontest.c
+++ b/tests/virnetdaemontest.c
@@ -179,8 +179,7 @@ testCreateServer(const char *server_name, const char *host, int family)
     return srv;
 
  error:
-    virObjectUnref(srv);
-    srv = NULL;
+    g_clear_pointer(&srv, virObjectUnref);
     goto cleanup;
 }
 
diff --git a/tests/virnetsockettest.c b/tests/virnetsockettest.c
index 33b8755ab7..447cbee89c 100644
--- a/tests/virnetsockettest.c
+++ b/tests/virnetsockettest.c
@@ -272,8 +272,7 @@ testSocketAccept(const void *opaque)
         goto join;
     }
 
-    virObjectUnref(ssock);
-    ssock = NULL;
+    g_clear_pointer(&ssock, virObjectUnref);
 
     ret = 0;
 
diff --git a/tests/virnettlshelpers.c b/tests/virnettlshelpers.c
index c9e2b372bb..ca293eb6d6 100644
--- a/tests/virnettlshelpers.c
+++ b/tests/virnettlshelpers.c
@@ -428,8 +428,7 @@ void testTLSDiscardCert(struct testTLSCertReq *req)
     if (!req->crt)
         return;
 
-    gnutls_x509_crt_deinit(req->crt);
-    req->crt = NULL;
+    g_clear_pointer(&req->crt, gnutls_x509_crt_deinit);
 
     if (getenv("VIRT_TEST_DEBUG_CERTS") == NULL)
         unlink(req->filename);
diff --git a/tests/virpcivpdtest.c b/tests/virpcivpdtest.c
index a5f4abae6f..3fd40b518c 100644
--- a/tests/virpcivpdtest.c
+++ b/tests/virpcivpdtest.c
@@ -227,7 +227,7 @@ testPCIVPDResourceCustomCompareIndex(const void *data G_GNUC_UNUSED)
         return -1;
 
     /* Different index, same value pointers */
-    g_free(b->value);
+    g_clear_pointer(&b->value, g_free);
     b->value = a->value;
     if (virPCIVPDResourceCustomCompareIndex(b, a)) {
         b->value = NULL;
diff --git a/tests/virusbtest.c b/tests/virusbtest.c
index 74333f0105..65050d5e5f 100644
--- a/tests/virusbtest.c
+++ b/tests/virusbtest.c
@@ -171,8 +171,7 @@ testUSBList(const void *opaque G_GNUC_UNUSED)
         dev = NULL;
     }
 
-    virObjectUnref(devlist);
-    devlist = NULL;
+    g_clear_pointer(&devlist, virObjectUnref);
 
     ndevs = virUSBDeviceListCount(list);
     if (testCheckNdevs("After first loop", ndevs, EXPECTED_NDEVS_ONE) < 0)
@@ -210,8 +209,7 @@ testUSBList(const void *opaque G_GNUC_UNUSED)
     }
 
     virUSBDeviceListDel(list, dev);
-    virUSBDeviceFree(dev);
-    dev = NULL;
+    g_clear_pointer(&dev, virUSBDeviceFree);
 
     if (testCheckNdevs("After deleting one",
                        virUSBDeviceListCount(list),
diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index eb3e0ef11a..246e8a16c0 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -1740,8 +1740,7 @@ virshDomainListCollect(vshControl *ctl, unsigned int flags)
 
  remove_entry:
         /* the domain has to be removed as it failed one of the filters */
-        virshDomainFree(list->domains[i]);
-        list->domains[i] = NULL;
+        g_clear_pointer(&list->domains[i], virshDomainFree);
         deleted++;
     }
 
@@ -1762,8 +1761,7 @@ virshDomainListCollect(vshControl *ctl, unsigned int flags)
         VIR_FREE(names[i]);
 
     if (!success) {
-        virshDomainListFree(list);
-        list = NULL;
+        g_clear_pointer(&list, virshDomainListFree);
     }
 
     VIR_FREE(names);
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index 43d310f2af..f959941664 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -12614,8 +12614,7 @@ virshUpdateDiskXML(xmlNodePtr disk_node,
 
         /* remove current source */
         xmlUnlinkNode(source);
-        xmlFreeNode(source);
-        source = NULL;
+        g_clear_pointer(&source, xmlFreeNode);
     }
 
     /* set the correct disk type */
diff --git a/tools/virsh-interface.c b/tools/virsh-interface.c
index 0a8539da71..55d3532c55 100644
--- a/tools/virsh-interface.c
+++ b/tools/virsh-interface.c
@@ -302,8 +302,7 @@ virshInterfaceListCollect(vshControl *ctl,
     VIR_FREE(inactiveNames);
 
     if (!success) {
-        virshInterfaceListFree(list);
-        list = NULL;
+        g_clear_pointer(&list, virshInterfaceListFree);
     }
 
     return list;
diff --git a/tools/virsh-network.c b/tools/virsh-network.c
index b2daf31d6b..97a160f772 100644
--- a/tools/virsh-network.c
+++ b/tools/virsh-network.c
@@ -640,8 +640,7 @@ virshNetworkListCollect(vshControl *ctl,
     VIR_FREE(names);
 
     if (!success) {
-        virshNetworkListFree(list);
-        list = NULL;
+        g_clear_pointer(&list, virshNetworkListFree);
     }
 
     return list;
@@ -1697,8 +1696,7 @@ virshNetworkPortListCollect(vshControl *ctl,
 
  cleanup:
     if (!success) {
-        virshNetworkPortListFree(list);
-        list = NULL;
+        g_clear_pointer(&list, virshNetworkPortListFree);
     }
 
     return list;
diff --git a/tools/virsh-nodedev.c b/tools/virsh-nodedev.c
index fe15d2d702..d01bdf699c 100644
--- a/tools/virsh-nodedev.c
+++ b/tools/virsh-nodedev.c
@@ -343,8 +343,7 @@ virshNodeDeviceListCollect(vshControl *ctl,
     VIR_FREE(names);
 
     if (!success) {
-        virshNodeDeviceListFree(list);
-        list = NULL;
+        g_clear_pointer(&list, virshNodeDeviceListFree);
     }
 
     return list;
diff --git a/tools/virsh-nwfilter.c b/tools/virsh-nwfilter.c
index 09ceaf6ec9..5a9e57e3f5 100644
--- a/tools/virsh-nwfilter.c
+++ b/tools/virsh-nwfilter.c
@@ -329,8 +329,7 @@ virshNWFilterListCollect(vshControl *ctl,
     VIR_FREE(names);
 
     if (!success) {
-        virshNWFilterListFree(list);
-        list = NULL;
+        g_clear_pointer(&list, virshNWFilterListFree);
     }
 
     return list;
@@ -692,8 +691,7 @@ virshNWFilterBindingListCollect(vshControl *ctl,
 
  cleanup:
     if (!success) {
-        virshNWFilterBindingListFree(list);
-        list = NULL;
+        g_clear_pointer(&list, virshNWFilterBindingListFree);
     }
 
     return list;
diff --git a/tools/virsh-pool.c b/tools/virsh-pool.c
index d391257f6e..7e7a8d511f 100644
--- a/tools/virsh-pool.c
+++ b/tools/virsh-pool.c
@@ -1012,8 +1012,7 @@ virshStoragePoolListCollect(vshControl *ctl,
         VIR_FREE(names[i]);
 
     if (!success) {
-        virshStoragePoolListFree(list);
-        list = NULL;
+        g_clear_pointer(&list, virshStoragePoolListFree);
     }
 
     VIR_FREE(names);
diff --git a/tools/virsh-secret.c b/tools/virsh-secret.c
index dff2710928..842647cb17 100644
--- a/tools/virsh-secret.c
+++ b/tools/virsh-secret.c
@@ -512,8 +512,7 @@ virshSecretListCollect(vshControl *ctl,
     }
 
     if (!success) {
-        virshSecretListFree(list);
-        list = NULL;
+        g_clear_pointer(&list, virshSecretListFree);
     }
 
     return list;
diff --git a/tools/virsh-snapshot.c b/tools/virsh-snapshot.c
index 154e82b48b..1c31eef7b9 100644
--- a/tools/virsh-snapshot.c
+++ b/tools/virsh-snapshot.c
@@ -1213,8 +1213,8 @@ virshSnapshotListCollect(vshControl *ctl, virDomainPtr dom,
                            STRNEQ_NULLABLE(fromname,
                                            snaplist->snaps[i].parent)))) ||
                 (roots && snaplist->snaps[i].parent)) {
-                virshDomainSnapshotFree(snaplist->snaps[i].snap);
-                snaplist->snaps[i].snap = NULL;
+                g_clear_pointer(&snaplist->snaps[i].snap,
+                                virshDomainSnapshotFree);
                 VIR_FREE(snaplist->snaps[i].parent);
                 deleted++;
             }
@@ -1241,8 +1241,8 @@ virshSnapshotListCollect(vshControl *ctl, virDomainPtr dom,
         for (i = 0; i < count; i++) {
             if (i == start_index || !snaplist->snaps[i].parent) {
                 VIR_FREE(names[i]);
-                virshDomainSnapshotFree(snaplist->snaps[i].snap);
-                snaplist->snaps[i].snap = NULL;
+                g_clear_pointer(&snaplist->snaps[i].snap,
+                                virshDomainSnapshotFree);
                 VIR_FREE(snaplist->snaps[i].parent);
                 deleted++;
             } else if (STREQ(snaplist->snaps[i].parent, fromname)) {
@@ -1279,8 +1279,8 @@ virshSnapshotListCollect(vshControl *ctl, virDomainPtr dom,
                 if (!found_parent) {
                     changed = true;
                     VIR_FREE(names[i]);
-                    virshDomainSnapshotFree(snaplist->snaps[i].snap);
-                    snaplist->snaps[i].snap = NULL;
+                    g_clear_pointer(&snaplist->snaps[i].snap,
+                                    virshDomainSnapshotFree);
                     VIR_FREE(snaplist->snaps[i].parent);
                     deleted++;
                 }
@@ -1302,8 +1302,8 @@ virshSnapshotListCollect(vshControl *ctl, virDomainPtr dom,
             case 1:
                 break;
             case 0:
-                virshDomainSnapshotFree(snaplist->snaps[i].snap);
-                snaplist->snaps[i].snap = NULL;
+                g_clear_pointer(&snaplist->snaps[i].snap,
+                                virshDomainSnapshotFree);
                 VIR_FREE(snaplist->snaps[i].parent);
                 deleted++;
                 break;
diff --git a/tools/virsh-volume.c b/tools/virsh-volume.c
index ca40fbcd72..d005602fe8 100644
--- a/tools/virsh-volume.c
+++ b/tools/virsh-volume.c
@@ -1298,8 +1298,7 @@ virshStorageVolListCollect(vshControl *ctl,
     VIR_FREE(names);
 
     if (!success) {
-        virshStorageVolListFree(list);
-        list = NULL;
+        g_clear_pointer(&list, virshStorageVolListFree);
     }
 
     return list;
diff --git a/tools/virsh.c b/tools/virsh.c
index 5234a3decb..1c75a66fcb 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -174,8 +174,7 @@ virshConnect(vshControl *ctl, const char *uri, bool readonly)
             vshError(ctl, "%s",
                      _("Cannot setup keepalive on connection "
                        "as requested, disconnecting"));
-            virConnectClose(c);
-            c = NULL;
+            g_clear_pointer(&c, virConnectClose);
             goto cleanup;
         }
         vshDebug(ctl, VSH_ERR_INFO, "%s",
diff --git a/tools/vsh.c b/tools/vsh.c
index c0098054e0..5056d7e19d 100644
--- a/tools/vsh.c
+++ b/tools/vsh.c
@@ -199,8 +199,7 @@ vshSaveLibvirtHelperError(void)
 void
 vshResetLibvirtError(void)
 {
-    virFreeError(last_error);
-    last_error = NULL;
+    g_clear_pointer(&last_error, virFreeError);
     virResetLastError();
 }
 
@@ -1377,8 +1376,7 @@ vshCommandParse(vshControl *ctl, vshCommandParser *parser, vshCmd **partial)
     const vshCmdDef *cmd = NULL;
 
     if (!partial) {
-        vshCommandFree(ctl->cmd);
-        ctl->cmd = NULL;
+        g_clear_pointer(&ctl->cmd, vshCommandFree);
     }
 
     while (1) {
@@ -1393,8 +1391,7 @@ vshCommandParse(vshControl *ctl, vshCommandParser *parser, vshCmd **partial)
         first = NULL;
 
         if (partial) {
-            vshCommandFree(*partial);
-            *partial = NULL;
+            g_clear_pointer(partial, vshCommandFree);
         }
 
         while (1) {
@@ -1605,8 +1602,7 @@ vshCommandParse(vshControl *ctl, vshCommandParser *parser, vshCmd **partial)
 
         *partial = tmp;
     } else {
-        vshCommandFree(ctl->cmd);
-        ctl->cmd = NULL;
+        g_clear_pointer(&ctl->cmd, vshCommandFree);
         vshCommandOptFree(first);
     }
     VIR_FREE(tkdata);
@@ -2731,8 +2727,7 @@ vshReadlineParse(const char *text, int state)
         const vshCmdOptDef *opt = NULL;
         g_autofree char *line = g_strdup(rl_line_buffer);
 
-        g_strfreev(list);
-        list = NULL;
+        g_clear_pointer(&list, g_strfreev);
         list_index = 0;
 
         *(line + rl_point) = '\0';
@@ -2798,8 +2793,7 @@ vshReadlineParse(const char *text, int state)
 
  cleanup:
     if (!ret) {
-        g_strfreev(list);
-        list = NULL;
+        g_clear_pointer(&list, g_strfreev);
         list_index = 0;
     }
 
-- 
2.34.1




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux