From bc817c8348fd903facd1d0e166380c763b00eb39 Mon Sep 17 00:00:00 2001 From: Mohaned Yossry Date: Wed, 18 Nov 2026 15:48:00 +0200 Subject: [PATCH] fix(macos): stop a second discovery crashing in release builds IOBluetoothDeviceInquiry does not retain its delegate, but its dealloc still touches it. startDiscovery() replaced inquiryDelegate before releasing the previous inquiry, so the old inquiry was freed while pointing at a freed delegate and crashed with EXC_BAD_ACCESS in -[IOBluetoothDeviceInquiry dealloc]. Detach, stop and release the previous inquiry before creating the next one. --- CHANGELOG.md | 12 ++++++++++++ .../FlutterClassicBluetoothPlugin.swift | 11 +++++++++++ 2 files changed, 23 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a676502..a7b38b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,15 @@ +## 1.7.5 + +### Fixed + +- **A second `startDiscovery()` crashed release builds on macOS.** Starting a + new inquiry replaced the old inquiry's delegate before releasing the old + `IOBluetoothDeviceInquiry`. The inquiry does not retain its delegate, so its + `dealloc` touched freed memory and the app died with `EXC_BAD_ACCESS` in + `-[IOBluetoothDeviceInquiry dealloc]`. Debug builds usually survived because + the delegate was freed later. The previous inquiry is now detached from its + delegate, stopped and released before the new one is created. + ## 1.7.4 ### Fixed 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..9213d04 100644 --- a/macos/flutter_classic_bluetooth/Sources/flutter_classic_bluetooth/FlutterClassicBluetoothPlugin.swift +++ b/macos/flutter_classic_bluetooth/Sources/flutter_classic_bluetooth/FlutterClassicBluetoothPlugin.swift @@ -217,6 +217,17 @@ public class FlutterClassicBluetoothPlugin: NSObject, FlutterPlugin { result(nil) return } + // IOBluetoothDeviceInquiry does not retain its delegate, and its dealloc + // still touches it. Retire the previous inquiry while its delegate is + // alive, otherwise replacing `inquiryDelegate` first leaves the old + // inquiry pointing at freed memory and releasing it crashes. + if let previous = inquiry { + previous.delegate = nil + previous.stop() + inquiry = nil + } + inquiryDelegate = nil + discovering = true discoveryStateHandler?.eventSink?(true)