From 915985c9f9459aac9a8adae8c762f5206a959cf5 Mon Sep 17 00:00:00 2001 From: Konstantinos Filios Date: Fri, 22 Mar 2024 21:57:44 +0200 Subject: [PATCH 1/9] add some weak refs --- Examples/HubSamplePhone/Info.plist | 10 +++++----- Examples/SignalRClient.xcodeproj/project.pbxproj | 13 +++++-------- Sources/SignalRClient/HubConnectionBuilder.swift | 2 +- Sources/SignalRClient/LongPollingTransport.swift | 2 +- Sources/SignalRClient/WebsocketsTransport.swift | 2 +- 5 files changed, 13 insertions(+), 16 deletions(-) diff --git a/Examples/HubSamplePhone/Info.plist b/Examples/HubSamplePhone/Info.plist index f41381c..f87da23 100644 --- a/Examples/HubSamplePhone/Info.plist +++ b/Examples/HubSamplePhone/Info.plist @@ -20,6 +20,11 @@ 1 LSRequiresIPhoneOS + NSAppTransportSecurity + + NSAllowsArbitraryLoads + + UILaunchStoryboardName LaunchScreen UIMainStoryboardFile @@ -41,10 +46,5 @@ UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight - NSAppTransportSecurity - - NSAllowsArbitraryLoads - - diff --git a/Examples/SignalRClient.xcodeproj/project.pbxproj b/Examples/SignalRClient.xcodeproj/project.pbxproj index b603309..c1c1b81 100644 --- a/Examples/SignalRClient.xcodeproj/project.pbxproj +++ b/Examples/SignalRClient.xcodeproj/project.pbxproj @@ -74,8 +74,6 @@ 7391A94722510170007BEF23 /* TransferFormat.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7391A91922510170007BEF23 /* TransferFormat.swift */; }; 7391A94822510170007BEF23 /* HubProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7391A91A22510170007BEF23 /* HubProtocol.swift */; }; 7391A94922510170007BEF23 /* HubProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7391A91A22510170007BEF23 /* HubProtocol.swift */; }; - 7391A94A22510170007BEF23 /* Util.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7391A91B22510170007BEF23 /* Util.swift */; }; - 7391A94B22510170007BEF23 /* Util.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7391A91B22510170007BEF23 /* Util.swift */; }; 7391A94E22510170007BEF23 /* JSONHubProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7391A91D22510170007BEF23 /* JSONHubProtocol.swift */; }; 7391A94F22510170007BEF23 /* JSONHubProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7391A91D22510170007BEF23 /* JSONHubProtocol.swift */; }; 7391A95022510170007BEF23 /* HttpConnection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7391A91E22510170007BEF23 /* HttpConnection.swift */; }; @@ -207,7 +205,6 @@ 7391A91822510170007BEF23 /* NegotiationResponse.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NegotiationResponse.swift; sourceTree = ""; }; 7391A91922510170007BEF23 /* TransferFormat.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TransferFormat.swift; sourceTree = ""; }; 7391A91A22510170007BEF23 /* HubProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HubProtocol.swift; sourceTree = ""; }; - 7391A91B22510170007BEF23 /* Util.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Util.swift; sourceTree = ""; }; 7391A91D22510170007BEF23 /* JSONHubProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONHubProtocol.swift; sourceTree = ""; }; 7391A91E22510170007BEF23 /* HttpConnection.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HttpConnection.swift; sourceTree = ""; }; 7391A91F22510170007BEF23 /* ConnectionDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConnectionDelegate.swift; sourceTree = ""; }; @@ -380,7 +377,6 @@ 7391A91822510170007BEF23 /* NegotiationResponse.swift */, 7391A91922510170007BEF23 /* TransferFormat.swift */, 7391A91A22510170007BEF23 /* HubProtocol.swift */, - 7391A91B22510170007BEF23 /* Util.swift */, 7391A91D22510170007BEF23 /* JSONHubProtocol.swift */, 7391A91E22510170007BEF23 /* HttpConnection.swift */, 7391A91F22510170007BEF23 /* ConnectionDelegate.swift */, @@ -537,6 +533,7 @@ }; 5AEC70EC20310E3E00616DD2 = { CreatedOnToolsVersion = 9.2; + DevelopmentTeam = K8A9GZ95QZ; LastSwiftMigration = 1020; ProvisioningStyle = Automatic; }; @@ -629,7 +626,6 @@ 7391A92F22510170007BEF23 /* ServerInvocationHandler.swift in Sources */, 7391A94322510170007BEF23 /* HttpClientProtocol.swift in Sources */, 7391A93122510170007BEF23 /* DefaultHttpClient.swift in Sources */, - 7391A94B22510170007BEF23 /* Util.swift in Sources */, 7391A93F22510170007BEF23 /* HandshakeProtocol.swift in Sources */, 5ABD6A632439AE6B00BFA93D /* ReconnectableConnection.swift in Sources */, 7391A94722510170007BEF23 /* TransferFormat.swift in Sources */, @@ -667,7 +663,6 @@ 7391A92E22510170007BEF23 /* ServerInvocationHandler.swift in Sources */, 7391A94222510170007BEF23 /* HttpClientProtocol.swift in Sources */, 7391A93022510170007BEF23 /* DefaultHttpClient.swift in Sources */, - 7391A94A22510170007BEF23 /* Util.swift in Sources */, 7391A93E22510170007BEF23 /* HandshakeProtocol.swift in Sources */, 5ABD6A622439AE6B00BFA93D /* ReconnectableConnection.swift in Sources */, 7391A94622510170007BEF23 /* TransferFormat.swift in Sources */, @@ -1063,12 +1058,13 @@ CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; CODE_SIGN_IDENTITY = "iPhone Developer"; CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = K8A9GZ95QZ; FRAMEWORK_SEARCH_PATHS = "$(inherited)"; GCC_C_LANGUAGE_STANDARD = gnu11; INFOPLIST_FILE = HubSamplePhone/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 12.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = moozzyk.HubSamplePhone; + PRODUCT_BUNDLE_IDENTIFIER = moozzyk.HubSamplee.kostas; PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = iphoneos; SWIFT_VERSION = 5.0; @@ -1086,12 +1082,13 @@ CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; CODE_SIGN_IDENTITY = "iPhone Developer"; CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = K8A9GZ95QZ; FRAMEWORK_SEARCH_PATHS = "$(inherited)"; GCC_C_LANGUAGE_STANDARD = gnu11; INFOPLIST_FILE = HubSamplePhone/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 12.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = moozzyk.HubSamplePhone; + PRODUCT_BUNDLE_IDENTIFIER = moozzyk.HubSamplee.kostas; PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = iphoneos; SWIFT_VERSION = 5.0; diff --git a/Sources/SignalRClient/HubConnectionBuilder.swift b/Sources/SignalRClient/HubConnectionBuilder.swift index e0aba64..cd9de27 100644 --- a/Sources/SignalRClient/HubConnectionBuilder.swift +++ b/Sources/SignalRClient/HubConnectionBuilder.swift @@ -24,7 +24,7 @@ public class HubConnectionBuilder { private let httpConnectionOptions = HttpConnectionOptions() private let hubConnectionOptions = HubConnectionOptions() private var logger: Logger = NullLogger() - private var delegate: HubConnectionDelegate? + private weak var delegate: HubConnectionDelegate? private var reconnectPolicy: ReconnectPolicy = NoReconnectPolicy() private var permittedTransportTypes: TransportType = .all private var transportFactory: ((Logger, TransportType) -> TransportFactory) = diff --git a/Sources/SignalRClient/LongPollingTransport.swift b/Sources/SignalRClient/LongPollingTransport.swift index 6016dd1..7df521b 100644 --- a/Sources/SignalRClient/LongPollingTransport.swift +++ b/Sources/SignalRClient/LongPollingTransport.swift @@ -9,7 +9,7 @@ import Foundation public class LongPollingTransport: Transport { - public var delegate: TransportDelegate? + public weak var delegate: TransportDelegate? private let logger: Logger private let closeQueue = DispatchQueue(label: "LongPollingTransportCloseQueue") diff --git a/Sources/SignalRClient/WebsocketsTransport.swift b/Sources/SignalRClient/WebsocketsTransport.swift index 74416be..3df7acc 100644 --- a/Sources/SignalRClient/WebsocketsTransport.swift +++ b/Sources/SignalRClient/WebsocketsTransport.swift @@ -18,7 +18,7 @@ public class WebsocketsTransport: NSObject, Transport, URLSessionWebSocketDelega private var isTransportClosed = false - public var delegate: TransportDelegate? + public weak var delegate: TransportDelegate? public let inherentKeepAlive = false init(logger: Logger) { From 33814bca4f00b03e2f18b9f3382e093e37946db2 Mon Sep 17 00:00:00 2001 From: Konstantinos Filios Date: Sat, 23 Mar 2024 13:26:33 +0200 Subject: [PATCH 2/9] fix memory leak between HubConnection and HubConnectionDelegate --- Examples/HubSamplePhone/ViewController.swift | 31 +++++++++++-------- .../SignalRClient.xcodeproj/project.pbxproj | 20 +++++++----- Sources/SignalRClient/HubConnection.swift | 2 +- package-lock.json | 6 ++++ 4 files changed, 37 insertions(+), 22 deletions(-) create mode 100644 package-lock.json diff --git a/Examples/HubSamplePhone/ViewController.swift b/Examples/HubSamplePhone/ViewController.swift index ee8326f..9500ffc 100644 --- a/Examples/HubSamplePhone/ViewController.swift +++ b/Examples/HubSamplePhone/ViewController.swift @@ -11,7 +11,7 @@ import SignalRClient class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { // Update the Url accordingly - private let serverUrl = "http://192.168.86.250:5000/chat" // /chat or /chatLongPolling or /chatWebSockets + private let serverUrl = "http://127.0.0.1:5000/chat" // /chat or /chatLongPolling or /chatWebSockets private let dispatchQueue = DispatchQueue(label: "hubsamplephone.queue.dispatcheueuq") private var chatHubConnection: HubConnection? @@ -35,18 +35,23 @@ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSour alert.addTextField() { textField in textField.placeholder = "Name"} let OKAction = UIAlertAction(title: "OK", style: .default) { action in self.name = alert.textFields?.first?.text ?? "John Doe" - - self.chatHubConnectionDelegate = ChatHubConnectionDelegate(controller: self) - self.chatHubConnection = HubConnectionBuilder(url: URL(string: self.serverUrl)!) - .withLogging(minLogLevel: .debug) - .withAutoReconnect() - .withHubConnectionDelegate(delegate: self.chatHubConnectionDelegate!) - .build() - - self.chatHubConnection!.on(method: "NewMessage", callback: {(user: String, message: String) in - self.appendMessage(message: "\(user): \(message)") - }) - self.chatHubConnection!.start() + (1...10).forEach { item in + let randomTimeAwait = Double.random(in: 0.5..<0.7) * Double(Int.random(in: 1..<7)) + DispatchQueue.global().asyncAfter(deadline: .now() + randomTimeAwait) { [weak self] in + guard let self else { return } + self.chatHubConnectionDelegate = ChatHubConnectionDelegate(controller: self) + self.chatHubConnection = HubConnectionBuilder(url: URL(string: self.serverUrl)!) + .withLogging(minLogLevel: .debug) + .withAutoReconnect() + .withHubConnectionDelegate(delegate: self.chatHubConnectionDelegate!) + .build() + + self.chatHubConnection!.on(method: "NewMessage", callback: {(user: String, message: String) in + self.appendMessage(message: "\(user): \(message)") + }) + self.chatHubConnection!.start() + } + } } alert.addAction(OKAction) self.present(alert, animated: true) diff --git a/Examples/SignalRClient.xcodeproj/project.pbxproj b/Examples/SignalRClient.xcodeproj/project.pbxproj index c1c1b81..f1d8112 100644 --- a/Examples/SignalRClient.xcodeproj/project.pbxproj +++ b/Examples/SignalRClient.xcodeproj/project.pbxproj @@ -768,9 +768,10 @@ CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; - CODE_SIGN_IDENTITY = ""; + CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; DEFINES_MODULE = YES; + DEVELOPMENT_TEAM = ""; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; DYLIB_INSTALL_NAME_BASE = "@rpath"; @@ -795,9 +796,10 @@ CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; - CODE_SIGN_IDENTITY = ""; + CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; DEFINES_MODULE = YES; + DEVELOPMENT_TEAM = ""; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; DYLIB_INSTALL_NAME_BASE = "@rpath"; @@ -940,9 +942,10 @@ buildSettings = { APPLICATION_EXTENSION_API_ONLY = NO; CLANG_ENABLE_MODULES = YES; - CODE_SIGN_IDENTITY = ""; + CODE_SIGN_IDENTITY = "Apple Development"; COMBINE_HIDPI_IMAGES = YES; DEFINES_MODULE = YES; + DEVELOPMENT_TEAM = ""; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; DYLIB_INSTALL_NAME_BASE = "@rpath"; @@ -952,7 +955,7 @@ INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks"; MACOSX_DEPLOYMENT_TARGET = 10.10; - PRODUCT_BUNDLE_IDENTIFIER = moozzyk.SignalRClient; + PRODUCT_BUNDLE_IDENTIFIER = moozzyk.HubSamplePhone; PRODUCT_NAME = SignalRClient; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; @@ -965,9 +968,10 @@ buildSettings = { APPLICATION_EXTENSION_API_ONLY = NO; CLANG_ENABLE_MODULES = YES; - CODE_SIGN_IDENTITY = ""; + CODE_SIGN_IDENTITY = "Apple Development"; COMBINE_HIDPI_IMAGES = YES; DEFINES_MODULE = YES; + DEVELOPMENT_TEAM = ""; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; DYLIB_INSTALL_NAME_BASE = "@rpath"; @@ -977,7 +981,7 @@ INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks"; MACOSX_DEPLOYMENT_TARGET = 10.10; - PRODUCT_BUNDLE_IDENTIFIER = moozzyk.SignalRClient; + PRODUCT_BUNDLE_IDENTIFIER = moozzyk.HubSamplePhone; PRODUCT_NAME = SignalRClient; SKIP_INSTALL = YES; SWIFT_VERSION = 5.0; @@ -1064,7 +1068,7 @@ INFOPLIST_FILE = HubSamplePhone/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 12.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = moozzyk.HubSamplee.kostas; + PRODUCT_BUNDLE_IDENTIFIER = moozzyk.HubSamplePhone.kostas; PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = iphoneos; SWIFT_VERSION = 5.0; @@ -1088,7 +1092,7 @@ INFOPLIST_FILE = HubSamplePhone/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 12.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = moozzyk.HubSamplee.kostas; + PRODUCT_BUNDLE_IDENTIFIER = moozzyk.HubSamplePhone.kostas; PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = iphoneos; SWIFT_VERSION = 5.0; diff --git a/Sources/SignalRClient/HubConnection.swift b/Sources/SignalRClient/HubConnection.swift index 785e37d..f2ba1ca 100755 --- a/Sources/SignalRClient/HubConnection.swift +++ b/Sources/SignalRClient/HubConnection.swift @@ -24,7 +24,7 @@ public class HubConnection { private let logger: Logger private var connection: Connection - private var connectionDelegate: HubConnectionConnectionDelegate? + private weak var connectionDelegate: HubConnectionConnectionDelegate? private var hubProtocol: HubProtocol private let keepAliveIntervalInSeconds: Double? diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..e97eb96 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,6 @@ +{ + "name": "SignalR-Client-Swift", + "lockfileVersion": 3, + "requires": true, + "packages": {} +} From 1fbffd7c3b179f6fc979a57ad2b65ccd4b2368a8 Mon Sep 17 00:00:00 2001 From: Konstantinos Filios Date: Sat, 23 Mar 2024 13:28:57 +0200 Subject: [PATCH 3/9] fix signings --- Examples/SignalRClient.xcodeproj/project.pbxproj | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Examples/SignalRClient.xcodeproj/project.pbxproj b/Examples/SignalRClient.xcodeproj/project.pbxproj index f1d8112..94754df 100644 --- a/Examples/SignalRClient.xcodeproj/project.pbxproj +++ b/Examples/SignalRClient.xcodeproj/project.pbxproj @@ -533,7 +533,6 @@ }; 5AEC70EC20310E3E00616DD2 = { CreatedOnToolsVersion = 9.2; - DevelopmentTeam = K8A9GZ95QZ; LastSwiftMigration = 1020; ProvisioningStyle = Automatic; }; @@ -1062,13 +1061,13 @@ CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; CODE_SIGN_IDENTITY = "iPhone Developer"; CODE_SIGN_STYLE = Automatic; - DEVELOPMENT_TEAM = K8A9GZ95QZ; + DEVELOPMENT_TEAM = ""; FRAMEWORK_SEARCH_PATHS = "$(inherited)"; GCC_C_LANGUAGE_STANDARD = gnu11; INFOPLIST_FILE = HubSamplePhone/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 12.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = moozzyk.HubSamplePhone.kostas; + PRODUCT_BUNDLE_IDENTIFIER = moozzyk.HubSamplePhone; PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = iphoneos; SWIFT_VERSION = 5.0; @@ -1086,13 +1085,13 @@ CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; CODE_SIGN_IDENTITY = "iPhone Developer"; CODE_SIGN_STYLE = Automatic; - DEVELOPMENT_TEAM = K8A9GZ95QZ; + DEVELOPMENT_TEAM = ""; FRAMEWORK_SEARCH_PATHS = "$(inherited)"; GCC_C_LANGUAGE_STANDARD = gnu11; INFOPLIST_FILE = HubSamplePhone/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 12.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = moozzyk.HubSamplePhone.kostas; + PRODUCT_BUNDLE_IDENTIFIER = moozzyk.HubSamplePhone; PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = iphoneos; SWIFT_VERSION = 5.0; From c74f527c0ff890e6ca00e3951831928bec4016b5 Mon Sep 17 00:00:00 2001 From: Konstantinos Filios Date: Sat, 23 Mar 2024 13:31:41 +0200 Subject: [PATCH 4/9] fix test url --- Examples/HubSamplePhone/ViewController.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Examples/HubSamplePhone/ViewController.swift b/Examples/HubSamplePhone/ViewController.swift index 9500ffc..0cf117c 100644 --- a/Examples/HubSamplePhone/ViewController.swift +++ b/Examples/HubSamplePhone/ViewController.swift @@ -11,7 +11,7 @@ import SignalRClient class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { // Update the Url accordingly - private let serverUrl = "http://127.0.0.1:5000/chat" // /chat or /chatLongPolling or /chatWebSockets + private let serverUrl = "http://0.0.0.0:5000/chat" // /chat or /chatLongPolling or /chatWebSockets private let dispatchQueue = DispatchQueue(label: "hubsamplephone.queue.dispatcheueuq") private var chatHubConnection: HubConnection? From 76cdbc9ef69a8abecdcb93f9a0c167ac707fc34e Mon Sep 17 00:00:00 2001 From: Konstantinos Filios Date: Tue, 26 Mar 2024 09:42:57 +0200 Subject: [PATCH 5/9] rollback extra edited files --- Examples/HubSamplePhone/ViewController.swift | 36 ++++++++----------- .../SignalRClient.xcodeproj/project.pbxproj | 10 +++--- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/Examples/HubSamplePhone/ViewController.swift b/Examples/HubSamplePhone/ViewController.swift index 0cf117c..b35d077 100644 --- a/Examples/HubSamplePhone/ViewController.swift +++ b/Examples/HubSamplePhone/ViewController.swift @@ -11,7 +11,7 @@ import SignalRClient class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { // Update the Url accordingly - private let serverUrl = "http://0.0.0.0:5000/chat" // /chat or /chatLongPolling or /chatWebSockets + private let serverUrl = "http://127.0.0.1:5000/chat" // /chat or /chatLongPolling or /chatWebSockets private let dispatchQueue = DispatchQueue(label: "hubsamplephone.queue.dispatcheueuq") private var chatHubConnection: HubConnection? @@ -29,38 +29,32 @@ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSour self.chatTableView.delegate = self self.chatTableView.dataSource = self } - + override func viewDidAppear(_ animated: Bool) { let alert = UIAlertController(title: "Enter your Name", message:"", preferredStyle: UIAlertController.Style.alert) alert.addTextField() { textField in textField.placeholder = "Name"} let OKAction = UIAlertAction(title: "OK", style: .default) { action in self.name = alert.textFields?.first?.text ?? "John Doe" - (1...10).forEach { item in - let randomTimeAwait = Double.random(in: 0.5..<0.7) * Double(Int.random(in: 1..<7)) - DispatchQueue.global().asyncAfter(deadline: .now() + randomTimeAwait) { [weak self] in - guard let self else { return } - self.chatHubConnectionDelegate = ChatHubConnectionDelegate(controller: self) - self.chatHubConnection = HubConnectionBuilder(url: URL(string: self.serverUrl)!) - .withLogging(minLogLevel: .debug) - .withAutoReconnect() - .withHubConnectionDelegate(delegate: self.chatHubConnectionDelegate!) - .build() - - self.chatHubConnection!.on(method: "NewMessage", callback: {(user: String, message: String) in - self.appendMessage(message: "\(user): \(message)") - }) - self.chatHubConnection!.start() - } - } + self.chatHubConnectionDelegate = ChatHubConnectionDelegate(controller: self) + self.chatHubConnection = HubConnectionBuilder(url: URL(string: self.serverUrl)!) + .withLogging(minLogLevel: .debug) + .withAutoReconnect() + .withHubConnectionDelegate(delegate: self.chatHubConnectionDelegate!) + .build() + + self.chatHubConnection!.on(method: "NewMessage", callback: {(user: String, message: String) in + self.appendMessage(message: "\(user): \(message)") + }) + self.chatHubConnection!.start() } alert.addAction(OKAction) self.present(alert, animated: true) } - + override func viewWillDisappear(_ animated: Bool) { chatHubConnection?.stop() } - + override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } diff --git a/Examples/SignalRClient.xcodeproj/project.pbxproj b/Examples/SignalRClient.xcodeproj/project.pbxproj index 94754df..2fbc6f7 100644 --- a/Examples/SignalRClient.xcodeproj/project.pbxproj +++ b/Examples/SignalRClient.xcodeproj/project.pbxproj @@ -767,7 +767,7 @@ CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; - CODE_SIGN_IDENTITY = "Apple Development"; + CODE_SIGN_IDENTITY = ""; CODE_SIGN_STYLE = Automatic; DEFINES_MODULE = YES; DEVELOPMENT_TEAM = ""; @@ -795,7 +795,7 @@ CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; - CODE_SIGN_IDENTITY = "Apple Development"; + CODE_SIGN_IDENTITY = ""; CODE_SIGN_STYLE = Automatic; DEFINES_MODULE = YES; DEVELOPMENT_TEAM = ""; @@ -941,7 +941,7 @@ buildSettings = { APPLICATION_EXTENSION_API_ONLY = NO; CLANG_ENABLE_MODULES = YES; - CODE_SIGN_IDENTITY = "Apple Development"; + CODE_SIGN_IDENTITY = ""; COMBINE_HIDPI_IMAGES = YES; DEFINES_MODULE = YES; DEVELOPMENT_TEAM = ""; @@ -954,7 +954,7 @@ INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks"; MACOSX_DEPLOYMENT_TARGET = 10.10; - PRODUCT_BUNDLE_IDENTIFIER = moozzyk.HubSamplePhone; + PRODUCT_BUNDLE_IDENTIFIER = moozzyk.SignalRClient; PRODUCT_NAME = SignalRClient; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; @@ -967,7 +967,7 @@ buildSettings = { APPLICATION_EXTENSION_API_ONLY = NO; CLANG_ENABLE_MODULES = YES; - CODE_SIGN_IDENTITY = "Apple Development"; + CODE_SIGN_IDENTITY = ""; COMBINE_HIDPI_IMAGES = YES; DEFINES_MODULE = YES; DEVELOPMENT_TEAM = ""; From 77bf9fbb81839e533e9fd9e72686f8284591720a Mon Sep 17 00:00:00 2001 From: Konstantinos Filios Date: Tue, 26 Mar 2024 09:45:23 +0200 Subject: [PATCH 6/9] remove DEVELOPMENT_TEAM --- Examples/SignalRClient.xcodeproj/project.pbxproj | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/Examples/SignalRClient.xcodeproj/project.pbxproj b/Examples/SignalRClient.xcodeproj/project.pbxproj index 2fbc6f7..8ff25f5 100644 --- a/Examples/SignalRClient.xcodeproj/project.pbxproj +++ b/Examples/SignalRClient.xcodeproj/project.pbxproj @@ -770,7 +770,6 @@ CODE_SIGN_IDENTITY = ""; CODE_SIGN_STYLE = Automatic; DEFINES_MODULE = YES; - DEVELOPMENT_TEAM = ""; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; DYLIB_INSTALL_NAME_BASE = "@rpath"; @@ -798,7 +797,6 @@ CODE_SIGN_IDENTITY = ""; CODE_SIGN_STYLE = Automatic; DEFINES_MODULE = YES; - DEVELOPMENT_TEAM = ""; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; DYLIB_INSTALL_NAME_BASE = "@rpath"; @@ -944,7 +942,6 @@ CODE_SIGN_IDENTITY = ""; COMBINE_HIDPI_IMAGES = YES; DEFINES_MODULE = YES; - DEVELOPMENT_TEAM = ""; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; DYLIB_INSTALL_NAME_BASE = "@rpath"; @@ -970,7 +967,6 @@ CODE_SIGN_IDENTITY = ""; COMBINE_HIDPI_IMAGES = YES; DEFINES_MODULE = YES; - DEVELOPMENT_TEAM = ""; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; DYLIB_INSTALL_NAME_BASE = "@rpath"; @@ -980,7 +976,7 @@ INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks"; MACOSX_DEPLOYMENT_TARGET = 10.10; - PRODUCT_BUNDLE_IDENTIFIER = moozzyk.HubSamplePhone; + PRODUCT_BUNDLE_IDENTIFIER = moozzyk.SignalRClient; PRODUCT_NAME = SignalRClient; SKIP_INSTALL = YES; SWIFT_VERSION = 5.0; @@ -1061,7 +1057,6 @@ CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; CODE_SIGN_IDENTITY = "iPhone Developer"; CODE_SIGN_STYLE = Automatic; - DEVELOPMENT_TEAM = ""; FRAMEWORK_SEARCH_PATHS = "$(inherited)"; GCC_C_LANGUAGE_STANDARD = gnu11; INFOPLIST_FILE = HubSamplePhone/Info.plist; @@ -1085,7 +1080,6 @@ CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; CODE_SIGN_IDENTITY = "iPhone Developer"; CODE_SIGN_STYLE = Automatic; - DEVELOPMENT_TEAM = ""; FRAMEWORK_SEARCH_PATHS = "$(inherited)"; GCC_C_LANGUAGE_STANDARD = gnu11; INFOPLIST_FILE = HubSamplePhone/Info.plist; From 55ef919c50d22cdff3859b8f1502192b4ccb6510 Mon Sep 17 00:00:00 2001 From: Konstantinos Filios Date: Fri, 29 Mar 2024 21:05:01 +0200 Subject: [PATCH 7/9] remove wrong weak --- Sources/SignalRClient/HubConnection.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SignalRClient/HubConnection.swift b/Sources/SignalRClient/HubConnection.swift index f2ba1ca..785e37d 100755 --- a/Sources/SignalRClient/HubConnection.swift +++ b/Sources/SignalRClient/HubConnection.swift @@ -24,7 +24,7 @@ public class HubConnection { private let logger: Logger private var connection: Connection - private weak var connectionDelegate: HubConnectionConnectionDelegate? + private var connectionDelegate: HubConnectionConnectionDelegate? private var hubProtocol: HubProtocol private let keepAliveIntervalInSeconds: Double? From 398a4a3d999557ff4112ec32fba57dd28aa818d9 Mon Sep 17 00:00:00 2001 From: Konstantinos Filios Date: Fri, 29 Mar 2024 21:41:56 +0200 Subject: [PATCH 8/9] more leaks fix, from closure due to are passed by reference and self increases the reference count over the parent class --- Sources/SignalRClient/HubConnection.swift | 29 +++++++++++++------ .../SignalRClient/LongPollingTransport.swift | 9 ++++-- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/Sources/SignalRClient/HubConnection.swift b/Sources/SignalRClient/HubConnection.swift index 785e37d..bafb740 100755 --- a/Sources/SignalRClient/HubConnection.swift +++ b/Sources/SignalRClient/HubConnection.swift @@ -90,7 +90,8 @@ public class HubConnection { // TODO: add negative test (e.g. invalid protocol) let handshakeRequest = HandshakeProtocol.createHandshakeRequest(hubProtocol: hubProtocol) logger.log(logLevel: .debug, message: "Sending handshake request: \(handshakeRequest)") - connection.send(data: "\(handshakeRequest)".data(using: .utf8)!) { error in + connection.send(data: "\(handshakeRequest)".data(using: .utf8)!) { [weak self] error in + guard let self else { return } if let e = error { self.logger.log(logLevel: .error, message: "Sending handshake request failed: \(e)") // TODO: (BUG) if this fails when reconnecting the callback should not be called and there @@ -156,7 +157,8 @@ public class HubConnection { let invocationData = try hubProtocol.writeMessage(message: invocationMessage) resetKeepAlive() - connection.send(data: invocationData, sendDidComplete: { error in + connection.send(data: invocationData, sendDidComplete: { [weak self] error in + guard let self else { return } if error == nil { self.resetKeepAlive() } @@ -281,7 +283,8 @@ public class HubConnection { let cancelInvocationMessage = CancelInvocationMessage(invocationId: streamHandle.invocationId) do { let cancelInvocationData = try hubProtocol.writeMessage(message: cancelInvocationMessage) - connection.send(data: cancelInvocationData, sendDidComplete: {error in + connection.send(data: cancelInvocationData, sendDidComplete: { [weak self] error in + guard let self else { return } if let e = error { self.logger.log(logLevel: .error, message: "Sending cancellation of server side streaming hub returned error: \(e)") self.callbackQueue.async { @@ -312,7 +315,8 @@ public class HubConnection { let invocationMessage = invocationHandler.createInvocationMessage(invocationId: id, method: method, arguments: arguments, streamIds: []) let invocationData = try hubProtocol.writeMessage(message: invocationMessage) - connection.send(data: invocationData) { error in + connection.send(data: invocationData) { [weak self] error in + guard let self else { return } if let e = error { self.logger.log(logLevel: .error, message: "Invoking server hub method \(method) returned error: \(e)") self.failInvocationWithError(invocationHandler: invocationHandler, invocationId: id, error: e) @@ -364,17 +368,20 @@ public class HubConnection { // TODO: (BUG) if this fails when reconnecting the callback should not be called and there // will be no further reconnect attempts logger.log(logLevel: .error, message: "Parsing handshake response failed: \(e)") - callbackQueue.async { + callbackQueue.async { [weak self] in + guard let self else { return } self.delegate?.connectionDidFailToOpen(error: e) } return } if originalHandshakeStatus.isReconnect { - callbackQueue.async { + callbackQueue.async { [weak self] in + guard let self else { return } self.delegate?.connectionDidReconnect() } } else { - callbackQueue.async { + callbackQueue.async { [weak self] in + guard let self else { return } self.delegate?.connectionDidOpen(hubConnection: self) } resetKeepAlive() @@ -515,7 +522,10 @@ public class HubConnection { } logger.log(logLevel: .debug, message: "Resetting keep alive") keepAlivePingTask!.cancel() - keepAlivePingTask = DispatchWorkItem { self.sendKeepAlivePing() } + keepAlivePingTask = DispatchWorkItem { [weak self] in + guard let self else { return } + self.sendKeepAlivePing() + } hubConnectionQueue.asyncAfter(deadline: DispatchTime.now() + keepAliveInterval, execute: keepAlivePingTask!) } } @@ -531,7 +541,8 @@ public class HubConnection { do { let cachedPingMessage = try hubProtocol.writeMessage(message: PingMessage.instance) logger.log(logLevel: .debug, message: "Sending keep alive") - connection.send(data: cachedPingMessage, sendDidComplete: { error in + connection.send(data: cachedPingMessage, sendDidComplete: { [weak self] error in + guard let self else { return } if let error = error { self.logger.log(logLevel: .error, message: "Keep alive send error: \(error.localizedDescription)") } else { diff --git a/Sources/SignalRClient/LongPollingTransport.swift b/Sources/SignalRClient/LongPollingTransport.swift index 7df521b..2327a2e 100644 --- a/Sources/SignalRClient/LongPollingTransport.swift +++ b/Sources/SignalRClient/LongPollingTransport.swift @@ -44,6 +44,7 @@ public class LongPollingTransport: Transport { return } httpClient.post(url: url, body: data) { (responseOptional, errorOptional) in + //If for any reason you need self instance please added as weak to prevent ARC count up and lead to memory leak if let error = errorOptional { sendDidComplete(error) } else if let response = responseOptional { @@ -62,7 +63,8 @@ public class LongPollingTransport: Transport { closeCalled = true active = false self.logger.log(logLevel: .debug, message: "Sending LongPolling session DELETE request...") - self.httpClient?.delete(url: self.url!, completionHandler: { (_, errorOptional) in + self.httpClient?.delete(url: self.url!, completionHandler: { [weak self] (_, errorOptional) in + guard let self else { return } if let error = errorOptional { self.logger.log(logLevel: .error, message: "Error while DELETE-ing long polling session: \(error)") self.delegate?.transportDidClose(error) @@ -81,7 +83,10 @@ public class LongPollingTransport: Transport { if self.active { let pollUrl = self.getPollUrl() self.logger.log(logLevel: .debug, message: "Polling \(pollUrl)") - self.httpClient?.get(url: pollUrl, completionHandler: self.handlePollResponse(response:error:)) + self.httpClient?.get(url: pollUrl, completionHandler: { [weak self] response,error in + guard let self else { return } + self.handlePollResponse(response: response, error: error) + }) } else { self.logger.log(logLevel: .debug, message: "Long Polling transport polling complete.") self.close() From fe73574b83c4e23063963d8d8f8ef8e742ec0305 Mon Sep 17 00:00:00 2001 From: Konstantinos Filios Date: Fri, 29 Mar 2024 21:47:45 +0200 Subject: [PATCH 9/9] thread leak --- Sources/SignalRClient/LongPollingTransport.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/SignalRClient/LongPollingTransport.swift b/Sources/SignalRClient/LongPollingTransport.swift index 2327a2e..892b178 100644 --- a/Sources/SignalRClient/LongPollingTransport.swift +++ b/Sources/SignalRClient/LongPollingTransport.swift @@ -58,7 +58,8 @@ public class LongPollingTransport: Transport { } public func close() { - closeQueue.sync { + closeQueue.sync { [weak self] in + guard let self else { return } if !closeCalled { closeCalled = true active = false