Skip to content

fix(macos): stop crash when discovery is started a second time - #11

Merged
almasumdev merged 1 commit into
almasumdev:mainfrom
ahmeddhus:main
Nov 18, 2026
Merged

almasumdev merged 1 commit into
almasumdev:mainfrom
ahmeddhus:main

Conversation

@ahmeddhus

@ahmeddhus ahmeddhus commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Description:

What this changes

Starting discovery a second time in the same session crashed the app on
macOS with EXC_BAD_ACCESS inside -[IOBluetoothDeviceInquiry dealloc].
IOBluetoothDeviceInquiry keeps an unretained pointer to its delegate and
uses it when it is freed, but handleStartDiscovery 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.

Platforms touched

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

How it was tested

Tested on macOS 27.2.1 (MacBook Pro, Apple M4 Pro) in a POS app that scans
for Bluetooth printers. Scanning twice in a row crashed every time before
the fix and works after it.

No automated test covers this. The crash is a use-after-free that only
shows up when the freed memory is reused, and a start/stop/start unit
test passed on the old code too, so it was left out.

Checks

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

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.
@almasumdev
almasumdev merged commit a0ca109 into almasumdev:main Nov 18, 2026
11 checks passed
@almasumdev

Copy link
Copy Markdown
Owner

Thanks again @ahmeddhus. That's a nasty one to track down, and the explanation of why the delegate has to be detached first made the review easy. Detaching the old delegate also stops a late completion from ending the new scan, which is a nice extra.

Merged and released in 1.7.5, now on pub.dev. I added the changelog entry with a credit to you.

Two solid fixes in a week, really appreciated. Keep them coming whenever you hit something.

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