From 370b467b71d2fca4a1dd4da6532bb128f3df487e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 7 Feb 2026 03:45:47 +0000 Subject: [PATCH] Address code review feedback: fix memory lifecycle and improve error message - Move capture session lifecycle to main thread - Add strong capture of captureSession in asyncAfter block to prevent premature deallocation - Improve error message to inform user about potential impact and suggested action Co-authored-by: rustdesk <71636191+rustdesk@users.noreply.github.com> --- flutter/macos/Runner/MainFlutterWindow.swift | 35 +++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/flutter/macos/Runner/MainFlutterWindow.swift b/flutter/macos/Runner/MainFlutterWindow.swift index 7bac14d2c..53dec2742 100644 --- a/flutter/macos/Runner/MainFlutterWindow.swift +++ b/flutter/macos/Runner/MainFlutterWindow.swift @@ -216,23 +216,26 @@ class MainFlutterWindow: NSWindow { AVCaptureDevice.requestAccess(for: .audio, completionHandler: { granted in if granted { // Instantiate an audio capture session to trigger macOS registration - if let audioDevice = AVCaptureDevice.default(for: .audio) { - do { - let audioInput = try AVCaptureDeviceInput(device: audioDevice) - let captureSession = AVCaptureSession() - captureSession.beginConfiguration() - if captureSession.canAddInput(audioInput) { - captureSession.addInput(audioInput) + // This needs to run on main thread to ensure proper lifecycle + DispatchQueue.main.async { + if let audioDevice = AVCaptureDevice.default(for: .audio) { + do { + let audioInput = try AVCaptureDeviceInput(device: audioDevice) + let captureSession = AVCaptureSession() + captureSession.beginConfiguration() + if captureSession.canAddInput(audioInput) { + captureSession.addInput(audioInput) + } + captureSession.commitConfiguration() + // Start and immediately stop the session to trigger registration + captureSession.startRunning() + // Keep a strong reference and stop after a brief moment + DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [captureSession] in + captureSession.stopRunning() + } + } catch { + NSLog("[RustDesk] Error creating audio capture session for permission registration: %@. The app may not appear in System Settings > Privacy & Security > Microphone. Please verify permissions manually.", error.localizedDescription) } - captureSession.commitConfiguration() - // Start and immediately stop the session to trigger registration - captureSession.startRunning() - // Stop after a brief moment to ensure registration is complete - DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { - captureSession.stopRunning() - } - } catch { - NSLog("[RustDesk] Error creating audio capture session for permission registration: %@", error.localizedDescription) } } }