Repository navigation
fix(macos): stop crash when discovery is started a second time - #11
Merged
Merged
Conversation
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.
6 of 11 tasks
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description:
What this changes
Starting discovery a second time in the same session crashed the app on
macOS with
EXC_BAD_ACCESSinside-[IOBluetoothDeviceInquiry dealloc].IOBluetoothDeviceInquirykeeps an unretained pointer to its delegate anduses it when it is freed, but
handleStartDiscoveryreleased the olddelegate 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
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 analyzereports no issuesflutter testpassesCHANGELOG.mdupdated