From e64610a163c23ec862c5f65d1accdd10a6163d47 Mon Sep 17 00:00:00 2001 From: Jos Dehaes Date: Tue, 16 Nov 2021 21:20:42 +0100 Subject: [PATCH] RAII --- src/freebsd/btop_collect.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/freebsd/btop_collect.cpp b/src/freebsd/btop_collect.cpp index fafe30f..7e2be8d 100644 --- a/src/freebsd/btop_collect.cpp +++ b/src/freebsd/btop_collect.cpp @@ -53,6 +53,7 @@ tab-size = 4 #include #include #include +#include using std::clamp, std::string_literals::operator""s, std::cmp_equal, std::cmp_less, std::cmp_greater; using std::ifstream, std::numeric_limits, std::streamsize, std::round, std::max, std::min; @@ -473,8 +474,10 @@ namespace Mem { u_int64_t total_bytes_read; u_int64_t total_bytes_write; - cur.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo)); - last.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo)); + static std::unique_ptr curDevInfo (reinterpret_cast(std::calloc(1, sizeof(struct devinfo))), std::free); + static std::unique_ptr lastDevInfo (reinterpret_cast(std::calloc(1, sizeof(struct devinfo))), std::free); + cur.dinfo = curDevInfo.get(); + last.dinfo = lastDevInfo.get(); int n = devstat_getdevs(NULL, &cur); for (int i = 0; i < n; i++) { devstat_compute_statistics(&cur.dinfo->devices[i], NULL, etime, DSM_TOTAL_BYTES_READ, &total_bytes_read,