Skip to content

Commit

Permalink
fix: Fix memory leaks (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
ginokent authored Feb 2, 2025
2 parents 8215550 + 37e8446 commit 45f20e0
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions Sources/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -274,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
Expand Down Expand Up @@ -330,6 +333,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
let image = NSImage(size: size)

image.lockFocus()
defer { image.unlockFocus() }

// Drawing the background
NSColor.clear.set()
Expand Down Expand Up @@ -369,7 +373,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
}

Expand Down Expand Up @@ -454,6 +457,9 @@ class AppDelegate: NSObject, NSApplicationDelegate {
cpuLoad.append(idle)
}

let size = vm_size_t(numCPUInfo) * vm_size_t(MemoryLayout<integer_t>.stride)
vm_deallocate(mach_task_self_, vm_address_t(bitPattern: cpuInfo), size)

// Calculate usage
var overallUsage = 0.0

Expand Down

0 comments on commit 45f20e0

Please sign in to comment.