Skip to contentSkip to conversation

dbus: client: fix memory leaks and errors

PATCH Applied Bartosz Golaszewski 1 participant 5 messages Oct 7, 2024 – Oct 9, 2024 GPIO

lore
Share & tools
lore
#0 lore raw

cover dbus: client: fix memory leaks and errors

The daemon gets good memory leak coverage just by running it through valgrind and executing the gpiocli test-suite. The client is a bit trickier to test so there sure will be more fixes coming.

---
Bartosz Golaszewski (3):
dbus: client: notify: fix reference counting
dbus: client: notify: free chip and line lists at exit
dbus: client: monitor: free the line list at exit

 dbus/client/monitor.c | 1 +
 dbus/client/notify.c  | 6 ++++--
 2 files changed, 5 insertions(+), 2 deletions(-)

---
base-commit: 0e4198b9eef1eeedf8cf12e970b36d4e50a1da48
change-id: 20241007-dbus-memory-fixes-3c9529124907

Best regards,

Signature · 1 line
Bartosz Golaszewski <[email protected]>
#1 lore raw

PATCH 1/3 dbus: client: notify: fix reference counting

From: Bartosz Golaszewski <[email protected]>

There's a reference overflow when adding chips to the global list and an underflow when fetching existing chips from that list in connect_line(). Fix both issues.

Fixes a5ab76da1e0a ("dbus: add the D-Bus daemon, command-line client and tests")
---
 dbus/client/notify.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
dbus/client/notify.c +2 -2
index 5c73343..6d51a6f 100644
@@ -211,10 +211,10 @@ static void connect_line(gpointer elem, gpointer user_data)
 	if (data->scoped_chip) {
 		if (g_list_length(data->chips) == 0) {
 			chip = gpiodbus_object_get_chip(chip_obj);
-			data->chips = g_list_append(data->chips,
-						    g_object_ref(chip));
+			data->chips = g_list_append(data->chips, chip);
 		} else {
 			chip = g_list_first(data->chips)->data;
+			g_object_ref(chip);
 		}
 	} else {
 		chip = gpiodbus_object_get_chip(chip_obj);
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/3 dbus: client: notify: free chip and line lists at exit

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

We don't free the lists storing line and chip objects for the duration of the program when exiting. Fix it.

Note: these objects need to be kept alive for D-Bus events to work.

Fixes a5ab76da1e0a ("dbus: add the D-Bus daemon, command-line client and tests")
---
 dbus/client/notify.c | 2 ++
 1 file changed, 2 insertions(+)
dbus/client/notify.c +2 -0
index 6d51a6f..800dd24 100644
@@ -289,6 +289,8 @@ int gpiocli_notify_main(int argc, char **argv)
 
 	g_main_loop_run(loop);
 
+	g_list_free_full(data.lines, g_object_unref);
+	g_list_free_full(data.chips, g_object_unref);
 	g_bus_unwatch_name(watch_id);
 
 	return EXIT_SUCCESS;
Signature · 1 line
2.43.0
#3 lore raw

PATCH 3/3 dbus: client: monitor: free the line list at exit

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Free the list of line proxies stored for the duration of the program at exit to fix a memory leak.

Note: these objects need to be kept alive for D-Bus events to work.

Fixes a5ab76da1e0a ("dbus: add the D-Bus daemon, command-line client and tests")
---
 dbus/client/monitor.c | 1 +
 1 file changed, 1 insertion(+)
dbus/client/monitor.c +1 -0
index 3bd25b8..df7eb51 100644
@@ -185,6 +185,7 @@ int gpiocli_monitor_main(int argc, char **argv)
 
 	g_main_loop_run(loop);
 
+	g_list_free_full(data.lines, g_object_unref);
 	g_bus_unwatch_name(watch_id);
 
 	return EXIT_SUCCESS;
Signature · 1 line
2.43.0
author Applied
#4 lore raw
In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Quoted 5 lines · Bartosz Golaszewski

The daemon gets good memory leak coverage just by running it through valgrind and executing the gpiocli test-suite. The client is a bit trickier to test so there sure will be more fixes coming.

Applied, thanks!

[1/3] dbus: client: notify: fix reference counting

      commit: c87847b23b398d0b62185cd8191aaa09113debdb
[2/3] dbus: client: notify: free chip and line lists at exit
      commit: 85471d394bab3c95e7b968c8a3df9a7ff894fce4
[3/3] dbus: client: monitor: free the line list at exit
      commit: 392cae9e4233040e1d1a171552490fa9be7fd6dd

Best regards,

Signature · 1 line
Bartosz Golaszewski <[email protected]>