Using "Instruments" tool shows every log message from the SystemExtension creates a persistent allocation. Although it seems there is no retain cycle, the allocated resources are not freed, which might be caused by the way the function is invoked using golang instead of the usual swift invocation freeing allocations with zero retain counter . Using the "autoreleasepool" seems to solve it. the call tree not freed: 12 WireGuardNetworkExtension runtime.asmcgocall.abi0 11 WireGuardNetworkExtension _cgo_f1100e4bfc6f_Cfunc_callLogger 10 WireGuardNetworkExtension callLogger 9 WireGuardNetworkExtension @objc closure #1 in WireGuardAdapter.setupLogHandler() <reducted>/wireguard-apple/<compiler-generated>:0 8 WireGuardNetworkExtension closure #1 in WireGuardAdapter.setupLogHandler() <reducted>/wireguard-apple/Sources/WireGuardKit/WireGuardAdapter.swift:299 7 libswiftCore.dylib specialized static String._fromUTF8Repairing(_:) 6 libswiftCore.dylib specialized static String._uncheckedFromUTF8(_:asciiPreScanResult:) 5 libswiftCore.dylib specialized static __StringStorage.create(initializingFrom:isASCII:) 4 libswiftCore.dylib _allocateStringStorage(codeUnitCapacity:) 3 libswiftCore.dylib _swift_allocObject_ 2 libswiftCore.dylib _swift_allocObject_ 1 libswiftCore.dylib swift_slowAlloc 0 libsystem_malloc.dylib _malloc_zone_malloc
Signed-off-by: Roy Azachi <royaza1...@gmail.com> --- Sources/WireGuardKit/WireGuardAdapter.swift | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Sources/WireGuardKit/WireGuardAdapter.swift b/Sources/WireGuardKit/WireGuardAdapter.swift index 4cb2e2e..3828951 100644 --- a/Sources/WireGuardKit/WireGuardAdapter.swift +++ b/Sources/WireGuardKit/WireGuardAdapter.swift @@ -295,11 +295,12 @@ public class WireGuardAdapter { let unretainedSelf = Unmanaged<WireGuardAdapter>.fromOpaque(context) .takeUnretainedValue() + autoreleasepool { + let swiftString = String(cString: message).trimmingCharacters(in: .newlines) + let tunnelLogLevel = WireGuardLogLevel(rawValue: logLevel) ?? .verbose - let swiftString = String(cString: message).trimmingCharacters(in: .newlines) - let tunnelLogLevel = WireGuardLogLevel(rawValue: logLevel) ?? .verbose - - unretainedSelf.logHandler(tunnelLogLevel, swiftString) + unretainedSelf.logHandler(tunnelLogLevel, swiftString) + } } } -- 2.30.1 (Apple Git-130)