From e9bd6e576f89016fbb4ff0a7944a4f3cd8ca863d Mon Sep 17 00:00:00 2001 From: Andrej Mihajlov Date: Fri, 19 Jun 2020 12:42:35 +0200 Subject: [PATCH] Fix retain cycle between NWPathMonitor and PacketTunnelProvider See: https://www.marisibrothers.com/2017/04/memory-leak-in-swift-assigning-function.html Signed-off-by: Andrej Mihajlov Signed-off-by: Jason A. Donenfeld --- .../WireGuardNetworkExtension/PacketTunnelProvider.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift b/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift index 2f7062f..987c290 100644 --- a/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift +++ b/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift @@ -52,7 +52,9 @@ class PacketTunnelProvider: NEPacketTunnelProvider { startTunnelCompletionHandler(PacketTunnelProviderError.couldNotSetNetworkSettings) } else { self.networkMonitor = NWPathMonitor() - self.networkMonitor!.pathUpdateHandler = self.pathUpdate + self.networkMonitor!.pathUpdateHandler = { [weak self] path in + self?.pathUpdate(path: path) + } self.networkMonitor!.start(queue: DispatchQueue(label: "NetworkMonitor")) let fileDescriptor = (self.packetFlow.value(forKeyPath: "socket.fileDescriptor") as? Int32) ?? -1