From 45709e6168cd7a19684df051fce777aba962bcd9 Mon Sep 17 00:00:00 2001 From: Eduardo Gonzalez Date: Mon, 24 Aug 2026 16:04:29 +0200 Subject: [PATCH] gconnman_agent_test.cpp: Fix memory leak when test fails Use RAII smart pointer to take care of live management of GError object. Although the memory leak only happens when the test fail, it is good to always have Valgrind leaks equal to zero on the tests as reference. Fixes #48 Signed-off-by: Eduardo Gonzalez --- tests/gconnman_agent_test.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/gconnman_agent_test.cpp b/tests/gconnman_agent_test.cpp index 3fd89db..7f2f987 100644 --- a/tests/gconnman_agent_test.cpp +++ b/tests/gconnman_agent_test.cpp @@ -32,8 +32,8 @@ constexpr const char* AGENT_INTERFACE = "net.connman.Agent"; constexpr const char* RETRY_ERROR = "net.connman.Agent.Error.Retry"; auto call_agent(GDBusConnection* bus, const std::string& path, - const gchar* method, GVariant* args, GError** error) - -> GVariant* { + const gchar* method, GVariant* args, + GError** error) -> GVariant* { return g_dbus_connection_call_sync( bus, g_dbus_connection_get_unique_name(bus), path.c_str(), AGENT_INTERFACE, method, args, nullptr, G_DBUS_CALL_FLAGS_NONE, @@ -94,10 +94,12 @@ TEST(ConnmanAgent, ReportErrorIsAnswered) { g_variant_new("(os)", "/net/connman/service/does_not_exist", "invalid-key"), &error); + std::unique_ptr error_guard{error, + &g_error_free}; ASSERT_NE(reply, nullptr) << "ReportError was not answered within " << CALL_TIMEOUT_MS - << "ms: " << (error != nullptr ? error->message : ""); + << "ms: " << (error_guard != nullptr ? error_guard->message : ""); g_variant_unref(reply); } @@ -204,9 +206,11 @@ TEST(ConnmanAgent, CancelIsAnswered) { GError* error = nullptr; GVariant* reply = call_agent(bus, manager->internalAgentPath(), "Cancel", nullptr, &error); + std::unique_ptr error_guard{error, + &g_error_free}; ASSERT_NE(reply, nullptr) << "Cancel was not answered within " << CALL_TIMEOUT_MS - << "ms: " << (error != nullptr ? error->message : ""); + << "ms: " << (error_guard != nullptr ? error_guard->message : ""); g_variant_unref(reply); }