From e19951c34fa1f3b605f03d1a158279ec48a2d1fc Mon Sep 17 00:00:00 2001 From: XXIV <13811862+thechampagne@users.noreply.github.com> Date: Sun, 2 Jun 2024 14:22:22 +0300 Subject: [PATCH] fix memory leak --- sensors/sensors_darwin_cgo.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/sensors/sensors_darwin_cgo.go b/sensors/sensors_darwin_cgo.go index 407fc76c3..153cf0721 100644 --- a/sensors/sensors_darwin_cgo.go +++ b/sensors/sensors_darwin_cgo.go @@ -6,7 +6,10 @@ package sensors // #cgo LDFLAGS: -framework IOKit // #include "smc_darwin.h" import "C" -import "context" +import ( + "context" + "unsafe" +) func TemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) { temperatureKeys := []string{ @@ -38,9 +41,11 @@ func TemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) { defer C.gopsutil_v4_close_smc() for _, key := range temperatureKeys { + ckey := C.CString(key) + defer C.free(unsafe.Pointer(ckey)) temperatures = append(temperatures, TemperatureStat{ SensorKey: key, - Temperature: float64(C.gopsutil_v4_get_temperature(C.CString(key))), + Temperature: float64(C.gopsutil_v4_get_temperature(ckey)), }) } return temperatures, nil