From d5cb24fbeb19ae57507e1715aa402b2dc33f9b6b Mon Sep 17 00:00:00 2001 From: Jos Dehaes Date: Sun, 10 Oct 2021 20:23:11 +0200 Subject: [PATCH] RAII --- src/osx/btop_collect.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/osx/btop_collect.cpp b/src/osx/btop_collect.cpp index 7460fb9..bd6de26 100644 --- a/src/osx/btop_collect.cpp +++ b/src/osx/btop_collect.cpp @@ -516,6 +516,15 @@ namespace Mem { return bool(val); } + class IOObject { + public: + IOObject(string name, io_object_t obj) : name(name), object(obj) {} + virtual ~IOObject() { IOObjectRelease(object); } + private: + string name; + io_object_t &object; + }; + void collect_disk(unordered_flat_map &disks, unordered_flat_map &mapping) { io_registry_entry_t drive; io_iterator_t drive_list; @@ -531,7 +540,9 @@ namespace Mem { Logger::error("Error in IOServiceGetMatchingServices()"); return; } + auto d = IOObject("drive list", drive_list); // dummy var so it gets destroyed while ((drive = IOIteratorNext(drive_list)) != 0) { + auto dr = IOObject("drive", drive); io_registry_entry_t volumeRef; IORegistryEntryGetParentEntry(drive, kIOServicePlane, &volumeRef); if (volumeRef) { @@ -547,11 +558,7 @@ namespace Mem { if (disks.contains(mountpoint)) { auto& disk = disks.at(mountpoint); CFDictionaryRef properties; - if (IORegistryEntryCreateCFProperties(volumeRef, (CFMutableDictionaryRef *)&properties, kCFAllocatorDefault, 0)) { - Logger::error("Error in IORegistryEntryCreateCFProperties()"); - goto RETURN; // We must use a goto here to clean up drive_list - } - + IORegistryEntryCreateCFProperties(volumeRef, (CFMutableDictionaryRef *)&properties, kCFAllocatorDefault, 0); if (properties) { CFDictionaryRef statistics = (CFDictionaryRef)CFDictionaryGetValue(properties, CFSTR("Statistics")); if (statistics) { @@ -588,12 +595,7 @@ namespace Mem { } } } - /* Release. */ - IOObjectRelease(drive); } - RETURN: - /* Release. */ - IOObjectRelease(drive_list); } auto collect(const bool no_update) -> mem_info & {