Skip to content

fix(winrtble): release radio state handlers on final adapter drop - #477

Merged
qdot merged 1 commit into
deviceplug:devfrom
Raphiiko:fix/winrt-radio-cleanup-0.13.1
Sep 20, 2026
Merged

qdot merged 1 commit into
deviceplug:devfrom
Raphiiko:fix/winrt-radio-cleanup-0.13.1

Conversation

@Raphiiko

@Raphiiko Raphiiko commented Sep 20, 2026

Copy link
Copy Markdown
Contributor

Summary

This is the proposed fix for the Windows memory growth described in #476.

Astra wrote this patch and analyzed the memory dumps. I can confirm the difference in memory consumption in my tests, but I can't vouch for the implementation or whether this is the right fix.

Changes

The patch saves the Radio::StateChanged registration token and removes the handler when the last adapter clone is dropped. The callback holds a weak reference to the device manager instead of keeping it alive.

It leaves the scan-handler cleanup in btleplug 0.13.1 unchanged.

Testing

I ran my application with unpatched 0.13.1 for about eight hours, then with this patch for two hours, using the same five Bluetooth LE devices.

Unpatched Patched
Private memory at start 53 MiB 53 MiB
Private memory at end 1,477 MiB 56 MiB
Growth over matching periods About 182 MiB/hour About 0.37 MiB/hour

All five devices connected in both runs, with no connection failures logged during measurement. I haven't tested the patched build for eight hours.

Astra's analysis of the matching dumps found radio callbacks increasing from 18 to 3,198 in the unpatched run. With the patch, it found one at both the start and end.

The patch also adds three Windows Bluetooth regression tests covering handler removal, adapter clones, and cleanup after scanning. All three passed, along with the 42 existing library tests. Formatting and library lint checks passed.

Keep radio event registration ownership shared across adapter clones. Use a weak manager reference in the callback and unregister when the last adapter drops. Add Windows hardware regression tests for handler release, adapter clones, and scan cleanup.
@qdot

qdot commented Sep 20, 2026

Copy link
Copy Markdown
Contributor

Fix looks good, thanks!

@qdot
qdot changed the base branch from master to dev September 20, 2026 19:21
@qdot
qdot self-requested a review September 20, 2026 19:21
@qdot
qdot merged commit 69428c0 into deviceplug:dev Sep 20, 2026
5 checks passed
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