modules: use scope_exit for deletion to make code more robust

pull/2586/head
Tamino Bauknecht 2023-10-20 22:39:10 +02:00
parent 89e85db790
commit a0b63d6b1e
No known key found for this signature in database
GPG Key ID: 77837396BE935C6C
3 changed files with 26 additions and 6 deletions

View File

@ -6,12 +6,19 @@
#include <algorithm>
#include <sstream>
#include "util/scope_guard.hpp"
namespace {
using GDBusManager = std::unique_ptr<GDBusObjectManager, void (*)(GDBusObjectManager*)>;
auto generateManager() -> GDBusManager {
GError* error = nullptr;
waybar::util::scope_guard error_deleter([error]() {
if (error) {
g_error_free(error);
}
});
GDBusObjectManager* manager = g_dbus_object_manager_client_new_for_bus_sync(
G_BUS_TYPE_SYSTEM,
GDBusObjectManagerClientFlags::G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_DO_NOT_AUTO_START,
@ -19,7 +26,6 @@ auto generateManager() -> GDBusManager {
if (error) {
spdlog::error("g_dbus_object_manager_client_new_for_bus_sync() failed: {}", error->message);
g_error_free(error);
}
auto destructor = [](GDBusObjectManager* manager) {

View File

@ -2,6 +2,8 @@
#include <spdlog/spdlog.h>
#include "util/scope_guard.hpp"
namespace waybar::modules::SNI {
Host::Host(const std::size_t id, const Json::Value& config, const Bar& bar,
@ -57,17 +59,20 @@ void Host::nameVanished(const Glib::RefPtr<Gio::DBus::Connection>& conn, const G
void Host::proxyReady(GObject* src, GAsyncResult* res, gpointer data) {
GError* error = nullptr;
waybar::util::scope_guard error_deleter([error]() {
if (error != nullptr) {
g_error_free(error);
}
});
SnWatcher* watcher = sn_watcher_proxy_new_finish(res, &error);
if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
spdlog::error("Host: {}", error->message);
g_error_free(error);
return;
}
auto host = static_cast<SNI::Host*>(data);
host->watcher_ = watcher;
if (error != nullptr) {
spdlog::error("Host: {}", error->message);
g_error_free(error);
return;
}
sn_watcher_call_register_host(host->watcher_, host->object_path_.c_str(), host->cancellable_,
@ -76,16 +81,19 @@ void Host::proxyReady(GObject* src, GAsyncResult* res, gpointer data) {
void Host::registerHost(GObject* src, GAsyncResult* res, gpointer data) {
GError* error = nullptr;
waybar::util::scope_guard error_deleter([error]() {
if (error != nullptr) {
g_error_free(error);
}
});
sn_watcher_call_register_host_finish(SN_WATCHER(src), res, &error);
if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
spdlog::error("Host: {}", error->message);
g_error_free(error);
return;
}
auto host = static_cast<SNI::Host*>(data);
if (error != nullptr) {
spdlog::error("Host: {}", error->message);
g_error_free(error);
return;
}
g_signal_connect(host->watcher_, "item-registered", G_CALLBACK(&Host::itemRegistered), data);

View File

@ -2,6 +2,8 @@
#include <spdlog/spdlog.h>
#include "util/scope_guard.hpp"
using namespace waybar::modules::SNI;
Watcher::Watcher()
@ -29,6 +31,11 @@ Watcher::~Watcher() {
void Watcher::busAcquired(const Glib::RefPtr<Gio::DBus::Connection>& conn, Glib::ustring name) {
GError* error = nullptr;
waybar::util::scope_guard error_deleter([error]() {
if (error) {
g_error_free(error);
}
});
g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(watcher_), conn->gobj(),
"/StatusNotifierWatcher", &error);
if (error != nullptr) {
@ -36,7 +43,6 @@ void Watcher::busAcquired(const Glib::RefPtr<Gio::DBus::Connection>& conn, Glib:
if (error->code != 2) {
spdlog::error("Watcher: {}", error->message);
}
g_error_free(error);
return;
}
g_signal_connect_swapped(watcher_, "handle-register-item",