modules+util: fix actual (potential) memory leaks
parent
a0b63d6b1e
commit
ae748b2644
|
@ -69,7 +69,7 @@ class UPower : public AModule {
|
||||||
UpDevice *displayDevice;
|
UpDevice *displayDevice;
|
||||||
guint login1_id;
|
guint login1_id;
|
||||||
GDBusConnection *login1_connection;
|
GDBusConnection *login1_connection;
|
||||||
UPowerTooltip *upower_tooltip;
|
std::unique_ptr<UPowerTooltip> upower_tooltip;
|
||||||
std::string lastStatus;
|
std::string lastStatus;
|
||||||
bool showAltText;
|
bool showAltText;
|
||||||
bool upowerRunning;
|
bool upowerRunning;
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
#include <cstdlib> // malloc
|
#include <cstdlib> // malloc
|
||||||
|
|
||||||
#include "modules/cpu_usage.hpp"
|
#include "modules/cpu_usage.hpp"
|
||||||
|
#include "util/scope_guard.hpp"
|
||||||
|
|
||||||
#if defined(__NetBSD__) || defined(__OpenBSD__)
|
#if defined(__NetBSD__) || defined(__OpenBSD__)
|
||||||
#include <sys/sched.h>
|
#include <sys/sched.h>
|
||||||
|
@ -33,6 +34,11 @@ std::vector<std::tuple<size_t, size_t>> waybar::modules::CpuUsage::parseCpuinfo(
|
||||||
int ncpu = sysconf(_SC_NPROCESSORS_CONF);
|
int ncpu = sysconf(_SC_NPROCESSORS_CONF);
|
||||||
size_t sz = CPUSTATES * (ncpu + 1) * sizeof(pcp_time_t);
|
size_t sz = CPUSTATES * (ncpu + 1) * sizeof(pcp_time_t);
|
||||||
pcp_time_t *cp_time = static_cast<pcp_time_t *>(malloc(sz)), *pcp_time = cp_time;
|
pcp_time_t *cp_time = static_cast<pcp_time_t *>(malloc(sz)), *pcp_time = cp_time;
|
||||||
|
waybar::util::scope_guard cp_time_deleter([cp_time]() {
|
||||||
|
if (cp_time) {
|
||||||
|
free(cp_time);
|
||||||
|
}
|
||||||
|
});
|
||||||
#if defined(__NetBSD__)
|
#if defined(__NetBSD__)
|
||||||
int mib[] = {
|
int mib[] = {
|
||||||
CTL_KERN,
|
CTL_KERN,
|
||||||
|
@ -97,6 +103,5 @@ std::vector<std::tuple<size_t, size_t>> waybar::modules::CpuUsage::parseCpuinfo(
|
||||||
}
|
}
|
||||||
cpuinfo.emplace_back(single_cp_time[CP_IDLE], total);
|
cpuinfo.emplace_back(single_cp_time[CP_IDLE], total);
|
||||||
}
|
}
|
||||||
free(cp_time);
|
|
||||||
return cpuinfo;
|
return cpuinfo;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
|
#include "util/scope_guard.hpp"
|
||||||
|
|
||||||
waybar::modules::Custom::Custom(const std::string& name, const std::string& id,
|
waybar::modules::Custom::Custom(const std::string& name, const std::string& id,
|
||||||
const Json::Value& config)
|
const Json::Value& config)
|
||||||
: ALabel(config, "custom-" + name, id, "{}"),
|
: ALabel(config, "custom-" + name, id, "{}"),
|
||||||
|
@ -57,6 +59,11 @@ void waybar::modules::Custom::continuousWorker() {
|
||||||
}
|
}
|
||||||
thread_ = [this, cmd] {
|
thread_ = [this, cmd] {
|
||||||
char* buff = nullptr;
|
char* buff = nullptr;
|
||||||
|
waybar::util::scope_guard buff_deleter([buff]() {
|
||||||
|
if (buff) {
|
||||||
|
free(buff);
|
||||||
|
}
|
||||||
|
});
|
||||||
size_t len = 0;
|
size_t len = 0;
|
||||||
if (getline(&buff, &len, fp_) == -1) {
|
if (getline(&buff, &len, fp_) == -1) {
|
||||||
int exit_code = 1;
|
int exit_code = 1;
|
||||||
|
@ -90,7 +97,6 @@ void waybar::modules::Custom::continuousWorker() {
|
||||||
output_ = {0, output};
|
output_ = {0, output};
|
||||||
dp.emit();
|
dp.emit();
|
||||||
}
|
}
|
||||||
free(buff);
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,8 @@
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include "util/scope_guard.hpp"
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#include <playerctl/playerctl.h>
|
#include <playerctl/playerctl.h>
|
||||||
}
|
}
|
||||||
|
@ -117,6 +119,11 @@ Mpris::Mpris(const std::string& id, const Json::Value& config)
|
||||||
}
|
}
|
||||||
|
|
||||||
GError* error = nullptr;
|
GError* error = nullptr;
|
||||||
|
waybar::util::scope_guard error_deleter([error]() {
|
||||||
|
if (error) {
|
||||||
|
g_error_free(error);
|
||||||
|
}
|
||||||
|
});
|
||||||
manager = playerctl_player_manager_new(&error);
|
manager = playerctl_player_manager_new(&error);
|
||||||
if (error) {
|
if (error) {
|
||||||
throw std::runtime_error(fmt::format("unable to create MPRIS client: {}", error->message));
|
throw std::runtime_error(fmt::format("unable to create MPRIS client: {}", error->message));
|
||||||
|
@ -136,9 +143,7 @@ Mpris::Mpris(const std::string& id, const Json::Value& config)
|
||||||
} else {
|
} else {
|
||||||
GList* players = playerctl_list_players(&error);
|
GList* players = playerctl_list_players(&error);
|
||||||
if (error) {
|
if (error) {
|
||||||
auto e = fmt::format("unable to list players: {}", error->message);
|
throw std::runtime_error(fmt::format("unable to list players: {}", error->message));
|
||||||
g_error_free(error);
|
|
||||||
throw std::runtime_error(e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto p = players; p != NULL; p = p->next) {
|
for (auto p = players; p != NULL; p = p->next) {
|
||||||
|
@ -410,8 +415,7 @@ auto Mpris::onPlayerNameAppeared(PlayerctlPlayerManager* manager, PlayerctlPlaye
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
GError* error = nullptr;
|
mpris->player = playerctl_player_new_from_name(player_name, NULL);
|
||||||
mpris->player = playerctl_player_new_from_name(player_name, &error);
|
|
||||||
g_object_connect(mpris->player, "signal::play", G_CALLBACK(onPlayerPlay), mpris, "signal::pause",
|
g_object_connect(mpris->player, "signal::play", G_CALLBACK(onPlayerPlay), mpris, "signal::pause",
|
||||||
G_CALLBACK(onPlayerPause), mpris, "signal::stop", G_CALLBACK(onPlayerStop),
|
G_CALLBACK(onPlayerPause), mpris, "signal::stop", G_CALLBACK(onPlayerStop),
|
||||||
mpris, "signal::stop", G_CALLBACK(onPlayerStop), mpris, "signal::metadata",
|
mpris, "signal::stop", G_CALLBACK(onPlayerStop), mpris, "signal::metadata",
|
||||||
|
@ -478,6 +482,11 @@ auto Mpris::getPlayerInfo() -> std::optional<PlayerInfo> {
|
||||||
}
|
}
|
||||||
|
|
||||||
GError* error = nullptr;
|
GError* error = nullptr;
|
||||||
|
waybar::util::scope_guard error_deleter([error]() {
|
||||||
|
if (error) {
|
||||||
|
g_error_free(error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
char* player_status = nullptr;
|
char* player_status = nullptr;
|
||||||
auto player_playback_status = PLAYERCTL_PLAYBACK_STATUS_STOPPED;
|
auto player_playback_status = PLAYERCTL_PLAYBACK_STATUS_STOPPED;
|
||||||
|
@ -487,9 +496,7 @@ auto Mpris::getPlayerInfo() -> std::optional<PlayerInfo> {
|
||||||
if (player_name == "playerctld") {
|
if (player_name == "playerctld") {
|
||||||
GList* players = playerctl_list_players(&error);
|
GList* players = playerctl_list_players(&error);
|
||||||
if (error) {
|
if (error) {
|
||||||
auto e = fmt::format("unable to list players: {}", error->message);
|
throw std::runtime_error(fmt::format("unable to list players: {}", error->message));
|
||||||
g_error_free(error);
|
|
||||||
throw std::runtime_error(e);
|
|
||||||
}
|
}
|
||||||
// > get the list of players [..] in order of activity
|
// > get the list of players [..] in order of activity
|
||||||
// https://github.com/altdesktop/playerctl/blob/b19a71cb9dba635df68d271bd2b3f6a99336a223/playerctl/playerctl-common.c#L248-L249
|
// https://github.com/altdesktop/playerctl/blob/b19a71cb9dba635df68d271bd2b3f6a99336a223/playerctl/playerctl-common.c#L248-L249
|
||||||
|
@ -568,12 +575,16 @@ auto Mpris::getPlayerInfo() -> std::optional<PlayerInfo> {
|
||||||
|
|
||||||
errorexit:
|
errorexit:
|
||||||
spdlog::error("mpris[{}]: {}", info.name, error->message);
|
spdlog::error("mpris[{}]: {}", info.name, error->message);
|
||||||
g_error_free(error);
|
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Mpris::handleToggle(GdkEventButton* const& e) {
|
bool Mpris::handleToggle(GdkEventButton* const& e) {
|
||||||
GError* error = nullptr;
|
GError* error = nullptr;
|
||||||
|
waybar::util::scope_guard error_deleter([error]() {
|
||||||
|
if (error) {
|
||||||
|
g_error_free(error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
auto info = getPlayerInfo();
|
auto info = getPlayerInfo();
|
||||||
if (!info) return false;
|
if (!info) return false;
|
||||||
|
@ -603,7 +614,6 @@ bool Mpris::handleToggle(GdkEventButton* const& e) {
|
||||||
if (error) {
|
if (error) {
|
||||||
spdlog::error("mpris[{}]: error running builtin on-click action: {}", (*info).name,
|
spdlog::error("mpris[{}]: error running builtin on-click action: {}", (*info).name,
|
||||||
error->message);
|
error->message);
|
||||||
g_error_free(error);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -63,7 +63,7 @@ UPower::UPower(const std::string& id, const Json::Value& config)
|
||||||
box_.set_has_tooltip(tooltip_enabled);
|
box_.set_has_tooltip(tooltip_enabled);
|
||||||
if (tooltip_enabled) {
|
if (tooltip_enabled) {
|
||||||
// Sets the window to use when showing the tooltip
|
// Sets the window to use when showing the tooltip
|
||||||
upower_tooltip = new UPowerTooltip(iconSize, tooltip_spacing, tooltip_padding);
|
upower_tooltip = std::make_unique<UPowerTooltip>(iconSize, tooltip_spacing, tooltip_padding);
|
||||||
box_.set_tooltip_window(*upower_tooltip);
|
box_.set_tooltip_window(*upower_tooltip);
|
||||||
box_.signal_query_tooltip().connect(sigc::mem_fun(*this, &UPower::show_tooltip_callback));
|
box_.signal_query_tooltip().connect(sigc::mem_fun(*this, &UPower::show_tooltip_callback));
|
||||||
}
|
}
|
||||||
|
@ -72,14 +72,13 @@ UPower::UPower(const std::string& id, const Json::Value& config)
|
||||||
G_BUS_NAME_WATCHER_FLAGS_AUTO_START, upowerAppear,
|
G_BUS_NAME_WATCHER_FLAGS_AUTO_START, upowerAppear,
|
||||||
upowerDisappear, this, NULL);
|
upowerDisappear, this, NULL);
|
||||||
|
|
||||||
GError* error = NULL;
|
client = up_client_new_full(NULL, NULL);
|
||||||
client = up_client_new_full(NULL, &error);
|
|
||||||
if (client == NULL) {
|
if (client == NULL) {
|
||||||
throw std::runtime_error("Unable to create UPower client!");
|
throw std::runtime_error("Unable to create UPower client!");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connect to Login1 PrepareForSleep signal
|
// Connect to Login1 PrepareForSleep signal
|
||||||
login1_connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
|
login1_connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
|
||||||
if (!login1_connection) {
|
if (!login1_connection) {
|
||||||
throw std::runtime_error("Unable to connect to the SYSTEM Bus!...");
|
throw std::runtime_error("Unable to connect to the SYSTEM Bus!...");
|
||||||
} else {
|
} else {
|
||||||
|
@ -99,6 +98,7 @@ UPower::UPower(const std::string& id, const Json::Value& config)
|
||||||
}
|
}
|
||||||
|
|
||||||
UPower::~UPower() {
|
UPower::~UPower() {
|
||||||
|
if (displayDevice != NULL) g_object_unref(displayDevice);
|
||||||
if (client != NULL) g_object_unref(client);
|
if (client != NULL) g_object_unref(client);
|
||||||
if (login1_id > 0) {
|
if (login1_id > 0) {
|
||||||
g_dbus_connection_signal_unsubscribe(login1_connection, login1_id);
|
g_dbus_connection_signal_unsubscribe(login1_connection, login1_id);
|
||||||
|
|
|
@ -7,8 +7,7 @@ namespace {
|
||||||
class PrepareForSleep {
|
class PrepareForSleep {
|
||||||
private:
|
private:
|
||||||
PrepareForSleep() {
|
PrepareForSleep() {
|
||||||
GError *error = NULL;
|
login1_connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
|
||||||
login1_connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
|
|
||||||
if (!login1_connection) {
|
if (!login1_connection) {
|
||||||
spdlog::warn("Unable to connect to the SYSTEM Bus!...");
|
spdlog::warn("Unable to connect to the SYSTEM Bus!...");
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue