From cc09a28d96387fd8337fedb3fa0e7b613ce74d40 Mon Sep 17 00:00:00 2001 From: der richter Date: Fri, 24 Nov 2023 19:40:38 +0100 Subject: [PATCH] mac: fix a race condition when updating the window title the title is updated on the main thread (mandatory with cocoa) asynchronously, because otherwise it would either deadlock when done synchronously, lead to undefined behaviour or just crashes. the problem here is that the c string was only copied to an NSString within that asynchronous call, which potentially would access the pointer when it is accessed, modified or freed by another thread. it is only safe to access this pointer as long as the control callback wasn't returned yet. to fix this we move the copying and creation of the String from the c string pointer outside of the asynchronous call where the conversion of an untyped pointer to a typed pointer is done too. since the resulting String is a copy it's safe to be used in the asynchronous call. also reverting ee6ad40, since the problem was most likely an SDK problem or the very same problem as mentioned here. i retested the crash case again und can't reproduce it anymore. using a swift String again instead of an NSSstring. Fixes #12935 --- video/out/mac/common.swift | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/video/out/mac/common.swift b/video/out/mac/common.swift index aac705005f..3c01abe572 100644 --- a/video/out/mac/common.swift +++ b/video/out/mac/common.swift @@ -650,10 +650,9 @@ class Common: NSObject { focus.pointee = NSApp.isActive return VO_TRUE case VOCTRL_UPDATE_WINDOW_TITLE: - let titleData = data!.assumingMemoryBound(to: Int8.self) + let title = String(cString: data!.assumingMemoryBound(to: CChar.self)) DispatchQueue.main.async { - let title = NSString(utf8String: titleData) as String? - self.title = title ?? "Unknown Title" + self.title = title } return VO_TRUE default: