From a74f9b367d19a387cdf26d8f241ad925956ce9f1 Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Wed, 15 Jun 2022 12:06:41 +0200 Subject: [PATCH] Ensure the dispatch source only gets deallocated after the dispatch_source_cancel is done Signed-off-by: Claudio Cambra --- .../FinderSyncExt/LocalSocketClient.m | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/shell_integration/MacOSX/OwnCloudFinderSync/FinderSyncExt/LocalSocketClient.m b/shell_integration/MacOSX/OwnCloudFinderSync/FinderSyncExt/LocalSocketClient.m index 37c39cc7b..ab43a5df2 100644 --- a/shell_integration/MacOSX/OwnCloudFinderSync/FinderSyncExt/LocalSocketClient.m +++ b/shell_integration/MacOSX/OwnCloudFinderSync/FinderSyncExt/LocalSocketClient.m @@ -139,11 +139,24 @@ NSLog(@"Closing connection."); if(self.readSource) { + // Since dispatch_source_cancel works asynchronously, if we deallocate the dispatch source here then we can + // cause a crash. So instead we strongly hold a reference to the read source and deallocate it asynchronously + // with the handler. + __block dispatch_source_t previousReadSource = self.readSource; + dispatch_source_set_cancel_handler(self.readSource, ^{ + previousReadSource = nil; + }); dispatch_source_cancel(self.readSource); + // The readSource is still alive due to the other reference and will be deallocated by the cancel handler self.readSource = nil; } if(self.writeSource) { + // Same deal with the write source + __block dispatch_source_t previousWriteSource = self.writeSource; + dispatch_source_set_cancel_handler(self.writeSource, ^{ + previousWriteSource = nil; + }); dispatch_source_cancel(self.writeSource); self.writeSource = nil; }