Skip to content

fix(macos): stop a second discovery crashing in release builds - #12

Closed
mohanedy wants to merge 1 commit into
almasumdev:mainfrom
mohanedy:fix/macos-second-discovery-crash
Closed

mohanedy wants to merge 1 commit into
almasumdev:mainfrom
mohanedy:fix/macos-second-discovery-crash

Conversation

@mohanedy

Copy link
Copy Markdown

What this changes

On macOS, calling startDiscovery() a second time crashes release builds with EXC_BAD_ACCESS:

Exception Type:    EXC_BAD_ACCESS (SIGSEGV)
0   libobjc.A.dylib   objc_retain + 16
1   IOBluetooth       -[IOBluetoothDeviceInquiry dealloc] + 36
2   <app>             FlutterClassicBluetoothPlugin.handleStartDiscovery(result:) + 492
3   <app>             FlutterClassicBluetoothPlugin.handle(_:result:) + ...

IOBluetoothDeviceInquiry does not retain its delegate, but its dealloc still touches it. When the first inquiry has completed (discovering == false), handleStartDiscovery does:

  1. inquiryDelegate = delegate: the previous DeviceInquiryDelegate is released and freed, while the previous inquiry still points at it.
  2. self.inquiry = inquiry: the previous inquiry is released, its dealloc reaches the freed delegate, and the app crashes.

Debug builds usually survive because the delegate is freed later. With optimisation it is freed right away, so the crash happens every time.

The fix detaches the previous inquiry from its delegate, stops it and releases it before the delegate is replaced. The change is limited to macos/.../FlutterClassicBluetoothPlugin.swift, and the Dart API is unchanged.

Platforms touched

  • Android
  • Windows
  • macOS
  • Linux
  • iOS
  • Dart only

How it was tested

macOS 26.5 on Apple silicon (Mac16,8).

  • Before: a Flutter app built in release mode that scans for Bluetooth Classic printers crashed with the stack above when it started a second scan.
  • After: the example app builds in release mode (flutter build macos --release) with the patch. The crash fix has not yet been confirmed on hardware with a second scan.

Checks

  • dart format .
  • flutter analyze reports no issues
  • flutter test passes
  • CHANGELOG.md updated
  • Capability flags, README support matrix and dartdoc tables still agree (unchanged)

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.
@mohanedy

Copy link
Copy Markdown
Author

Closing as a duplicate of #11.

@mohanedy mohanedy closed this Nov 18, 2026
@mohanedy
mohanedy deleted the fix/macos-second-discovery-crash branch November 18, 2026 13:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants