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)