From fcc7ea30da2139deb3819f87cc9dc8a8a684f2d5 Mon Sep 17 00:00:00 2001 From: Ahmed Hussein Date: Wed, 18 Nov 2026 12:09:37 +0200 Subject: [PATCH] fix(macos): stop crash when discovery is started a second time Starting discovery again in the same session crashed the app with EXC_BAD_ACCESS inside -[IOBluetoothDeviceInquiry dealloc]. The inquiry keeps an unretained pointer to its delegate and uses it when it is freed, but the plugin released the old delegate before the old inquiry. Now the previous inquiry is stopped and detached from its delegate before both are released. A late completion callback from the old inquiry can also no longer mark a new scan as finished. Tested on macOS in a POS app: scanning for printers twice in a row crashed before the fix and works after it. --- .../FlutterClassicBluetoothPlugin.swift | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/macos/flutter_classic_bluetooth/Sources/flutter_classic_bluetooth/FlutterClassicBluetoothPlugin.swift b/macos/flutter_classic_bluetooth/Sources/flutter_classic_bluetooth/FlutterClassicBluetoothPlugin.swift index 01b4826..2104b7f 100644 --- a/macos/flutter_classic_bluetooth/Sources/flutter_classic_bluetooth/FlutterClassicBluetoothPlugin.swift +++ b/macos/flutter_classic_bluetooth/Sources/flutter_classic_bluetooth/FlutterClassicBluetoothPlugin.swift @@ -220,6 +220,12 @@ public class FlutterClassicBluetoothPlugin: NSObject, FlutterPlugin { discovering = true discoveryStateHandler?.eventSink?(true) + // The inquiry holds its delegate unretained and touches it in dealloc, + // so detach the previous inquiry before its delegate is released. + inquiry?.stop() + inquiry?.delegate = nil + inquiry = nil + let delegate = DeviceInquiryDelegate( onDeviceFound: { [weak self] device in guard let self = self else { return }