From f832f5976633f11c75f4b34d55007167ce05889f Mon Sep 17 00:00:00 2001 From: ginokent <29125616+ginokent@users.noreply.github.com> Date: Sun, 2 Feb 2025 18:38:13 +0900 Subject: [PATCH 1/3] refactor: Fix misc --- Sources/main.swift | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Sources/main.swift b/Sources/main.swift index e720735..fa96501 100644 --- a/Sources/main.swift +++ b/Sources/main.swift @@ -192,9 +192,10 @@ class AppDelegate: NSObject, NSApplicationDelegate { func restartPingTask() { DispatchQueue.main.async { [weak self] in - self?.pingTask?.terminate() - self?.pingTask = nil - self?.startPingTask() + guard let self = self else { return } + self.pingTask?.terminate() + self.pingTask = nil + self.startPingTask() } } @@ -330,6 +331,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { let image = NSImage(size: size) image.lockFocus() + defer { image.unlockFocus() } // Drawing the background NSColor.clear.set() @@ -369,7 +371,6 @@ class AppDelegate: NSObject, NSApplicationDelegate { drawFilledLineGraph(data: dataSets["Ping"]!, in: NSRect(x: xOffset, y: 0, width: graphWidth, height: height), maxValue: maxMillisecond, fillColor: pingFillColor, lineColor: pingLineColor) drawCenteredText(text: "Ping", in: NSRect(x: xOffset, y: 0, width: graphWidth, height: height), color: NSColor.labelColor) - image.unlockFocus() return image } From 1c00180bad42d18d0c8f77cca70d638214035ded Mon Sep 17 00:00:00 2001 From: ginokent <29125616+ginokent@users.noreply.github.com> Date: Sun, 2 Feb 2025 18:49:00 +0900 Subject: [PATCH 2/3] fix: deallocate vm_size_t to fix memory leak --- Sources/main.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sources/main.swift b/Sources/main.swift index fa96501..fa7d745 100644 --- a/Sources/main.swift +++ b/Sources/main.swift @@ -455,6 +455,9 @@ class AppDelegate: NSObject, NSApplicationDelegate { cpuLoad.append(idle) } + let size = vm_size_t(numCPUInfo) * vm_size_t(MemoryLayout.stride) + vm_deallocate(mach_task_self_, vm_address_t(bitPattern: cpuInfo), size) + // Calculate usage var overallUsage = 0.0 From 37e84462b4ec5bc4742f9fc62d6553b833083c21 Mon Sep 17 00:00:00 2001 From: ginokent <29125616+ginokent@users.noreply.github.com> Date: Sun, 2 Feb 2025 18:49:31 +0900 Subject: [PATCH 3/3] fix: Add autoreleasepool to fix memory leak --- Sources/main.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sources/main.swift b/Sources/main.swift index fa7d745..230f4f0 100644 --- a/Sources/main.swift +++ b/Sources/main.swift @@ -275,7 +275,9 @@ class AppDelegate: NSObject, NSApplicationDelegate { // Update the graph if let button = statusItem?.button { - button.image = createGraphImage(dataSets: dataPointDict) + autoreleasepool { + button.image = createGraphImage(dataSets: dataPointDict) + } } // Log output