From 0b3433d7322635868976e861b42afc88c327cc50 Mon Sep 17 00:00:00 2001
From: George Kiagiadakis <george.kiagiadakis@collabora.com>
Date: Tue, 18 Jun 2019 10:02:01 +0300
Subject: [PATCH] modules: fix some ref counting and memory {de-,}allocation
 issues

---
 modules/module-pipewire.c                  | 27 ++++++----------------
 modules/module-pw-alsa-udev.c              | 27 ++++++----------------
 modules/module-pw-audio-softdsp-endpoint.c | 16 ++++---------
 3 files changed, 19 insertions(+), 51 deletions(-)

diff --git a/modules/module-pipewire.c b/modules/module-pipewire.c
index 0cbfda74..ccdbdbcc 100644
--- a/modules/module-pipewire.c
+++ b/modules/module-pipewire.c
@@ -55,16 +55,10 @@ endpoint_info_destroy(gpointer p)
   struct endpoint_info *ei = p;
 
   /* Free the name */
-  if (ei->name) {
-    g_free (ei->name);
-    ei->name = NULL;
-  }
+  g_free (ei->name);
 
   /* Free the media class */
-  if (ei->media_class) {
-    g_free (ei->media_class);
-    ei->media_class = NULL;
-  }
+  g_free (ei->media_class);
 
   /* Clean up */
   g_slice_free (struct endpoint_info, p);
@@ -76,10 +70,7 @@ proxy_info_destroy(gpointer p)
   struct proxy_info *pi = p;
 
   /* Unref the proxy port */
-  if (pi->proxy_port) {
-    g_object_unref (pi->proxy_port);
-    pi->proxy_port = NULL;
-  }
+  g_clear_object (&pi->proxy_port);
 
   /* Clean up */
   g_slice_free (struct proxy_info, p);
@@ -122,8 +113,7 @@ proxy_node_created(GObject *initable, GAsyncResult *res, gpointer d)
   g_variant_builder_add (&b, "{sv}",
       "proxy-node", g_variant_new_uint64 ((guint64) proxy_node));
   g_variant_builder_add (&b, "{sv}",
-      "proxy-port", g_variant_new_uint64 ((guint64)
-          g_object_ref(pi->proxy_port)));
+      "proxy-port", g_variant_new_uint64 ((guint64) pi->proxy_port));
   endpoint_props = g_variant_builder_end (&b);
 
   /* Create the endpoint */
@@ -223,7 +213,7 @@ handle_node (struct module_data *data, uint32_t id, uint32_t parent_id,
       SPA_PARAM_Profile, 0, param);
 
   /* Create the endpoint info */
-  ei = g_new0(struct endpoint_info, 1);
+  ei = g_slice_new0 (struct endpoint_info);
   ei->name = g_strdup(name);
   ei->media_class = g_strdup(media_class);
   ei->proxy = proxy;
@@ -252,7 +242,7 @@ handle_port(struct module_data *data, uint32_t id, uint32_t parent_id,
     return;
 
   /* Create the port info */
-  pi = g_new0(struct proxy_info, 1);
+  pi = g_slice_new0 (struct proxy_info);
   pi->data = data;
   pi->node_id = parent_id;
   pi->proxy_port = NULL;
@@ -309,10 +299,7 @@ module_destroy (gpointer d)
   struct module_data *data = d;
 
   /* Destroy the hash table */
-  if (data->client_nodes_info) {
-    g_hash_table_destroy(data->client_nodes_info);
-    data->client_nodes_info = NULL;
-  }
+  g_hash_table_unref (data->client_nodes_info);
 
   /* Clean up */
   g_slice_free (struct module_data, data);
diff --git a/modules/module-pw-alsa-udev.c b/modules/module-pw-alsa-udev.c
index 5d8a0da1..e9ae39b9 100644
--- a/modules/module-pw-alsa-udev.c
+++ b/modules/module-pw-alsa-udev.c
@@ -49,16 +49,10 @@ endpoint_info_destroy(gpointer p)
   struct endpoint_info *ei = p;
 
   /* Free the name */
-  if (ei->name) {
-    g_free (ei->name);
-    ei->name = NULL;
-  }
+  g_free (ei->name);
 
   /* Free the media class */
-  if (ei->media_class) {
-    g_free (ei->media_class);
-    ei->media_class = NULL;
-  }
+  g_free (ei->media_class);
 
   /* Clean up */
   g_slice_free (struct endpoint_info, p);
@@ -70,10 +64,7 @@ proxy_info_destroy(gpointer p)
   struct proxy_info *pi = p;
 
   /* Unref the proxy port */
-  if (pi->proxy_port) {
-    g_object_unref (pi->proxy_port);
-    pi->proxy_port = NULL;
-  }
+  g_clear_object (&pi->proxy_port);
 
   /* Clean up */
   g_slice_free (struct proxy_info, p);
@@ -113,8 +104,7 @@ proxy_node_created(GObject *initable, GAsyncResult *res, gpointer data)
   g_variant_builder_add (&b, "{sv}",
       "proxy-node", g_variant_new_uint64 ((guint64) proxy_node));
   g_variant_builder_add (&b, "{sv}",
-      "proxy-port", g_variant_new_uint64 ((guint64)
-          g_object_ref(pi->proxy_port)));
+      "proxy-port", g_variant_new_uint64 ((guint64) pi->proxy_port));
   endpoint_props = g_variant_builder_end (&b);
 
   /* Create and register the endpoint */
@@ -181,7 +171,7 @@ handle_node(struct impl *impl, uint32_t id, uint32_t parent_id,
     return;
 
   /* Create the endpoint info */
-  ei = g_new0(struct endpoint_info, 1);
+  ei = g_slice_new0 (struct endpoint_info);
   ei->name = g_strdup(name);
   ei->media_class = g_strdup(media_class);
 
@@ -211,7 +201,7 @@ handle_port(struct impl *impl, uint32_t id, uint32_t parent_id,
     return;
 
   /* Create the port info */
-  pi = g_new0(struct proxy_info, 1);
+  pi = g_slice_new0 (struct proxy_info);
   pi->impl = impl;
   pi->node_id = parent_id;
   pi->proxy_port = NULL;
@@ -267,10 +257,7 @@ module_destroy (gpointer data)
   struct impl *impl = data;
 
   /* Destroy the hash table */
-  if (impl->alsa_nodes_info) {
-    g_hash_table_destroy(impl->alsa_nodes_info);
-    impl->alsa_nodes_info = NULL;
-  }
+  g_hash_table_unref (impl->alsa_nodes_info);
 
   /* Clean up */
   g_slice_free (struct impl, impl);
diff --git a/modules/module-pw-audio-softdsp-endpoint.c b/modules/module-pw-audio-softdsp-endpoint.c
index 23ccc210..9dac1dec 100644
--- a/modules/module-pw-audio-softdsp-endpoint.c
+++ b/modules/module-pw-audio-softdsp-endpoint.c
@@ -340,16 +340,10 @@ endpoint_finalize (GObject * object)
   WpPwAudioSoftdspEndpoint *self = WP_PW_AUDIO_SOFTDSP_ENDPOINT (object);
 
   /* Unref the proxy node */
-  if (self->proxy_node) {
-    g_object_unref(self->proxy_node);
-    self->proxy_node = NULL;
-  }
+  g_clear_object (&self->proxy_node);
 
   /* Unref the proxy port */
-  if (self->proxy_port) {
-    g_object_unref(self->proxy_port);
-    self->proxy_port = NULL;
-  }
+  g_clear_object (&self->proxy_port);
 
   /* Clear the dsp info */
   if (self->dsp_info) {
@@ -376,11 +370,11 @@ endpoint_set_property (GObject * object, guint property_id,
   switch (property_id) {
   case PROP_NODE_PROXY:
     g_clear_object(&self->proxy_node);
-    self->proxy_node = g_value_get_object(value);
+    self->proxy_node = g_value_dup_object(value);
     break;
   case PROP_PORT_PROXY:
     g_clear_object(&self->proxy_port);
-    self->proxy_port = g_value_get_object(value);
+    self->proxy_port = g_value_dup_object(value);
     break;
   default:
     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -572,7 +566,7 @@ static const struct pw_registry_proxy_events registry_events = {
 static gpointer
 endpoint_factory (WpFactory * factory, GType type, GVariant * properties)
 {
-  WpCore *wp_core = NULL;
+  g_autoptr (WpCore) wp_core = NULL;
   WpRemote *remote;
   struct pw_remote *pw_remote;
   const gchar *name = NULL;
-- 
GitLab