Repository navigation
Description
🔍 AI-generated bug report — drafted by GitHub Copilot CLI and verified with real local runs. Posting with full transparency; please review before triage.
Description
Microsoft.Maui.Devices.Sensors.Accelerometer.ReadingChanged is backed by a plain multicast delegate on the app-lifetime singleton Accelerometer.Default. Any subscriber that is not removed with -= is retained for the entire life of the process. Accelerometer.Stop() only stops the platform sensor — it does not clear the managed delegate.
A page / view-model that does the very common thing — subscribe in OnAppearing, call Stop() in OnDisappearing (and forget -=) — therefore leaks its entire object graph (page + view-model + payload) every time it is navigated away from.
By contrast, Application.RequestedThemeChanged uses a WeakEventManager, so the same mistake there does not leak. The Essentials static events (Accelerometer, Gyroscope, Magnetometer, Compass, OrientationSensor, Barometer, and also Connectivity / Battery) do not — that inconsistency is what this issue is about.
The leak is the
+=subscription itself and is independent ofStart()/ sensor hardware, so it reproduces on emulators and simulators too.
Retention path
static Accelerometer.Default (singleton)
-> ReadingChanged multicast delegate
-> subscriber page / view-model
-> payload (view-model state, captured services, etc.)
Relevant source: src/Essentials/src/Accelerometer/Accelerometer.shared.cs — the static facade forwards to Default; the implementation exposes a plain ReadingChanged event; Stop() / PlatformStop() do not clear delegates. The same shape exists for Gyroscope / Magnetometer / Compass / OrientationSensor / Barometer / Connectivity / Battery.
Steps to Reproduce
Repro project: src/Controls/samples/SensorSubscriptionLeakRepro on branch repro/sensor-subscription-leak.
- Build & run on any target — the harness auto-runs on launch and prints results bracketed by
>>>SENSOR_LEAK_REPRO>>>(and writessensorleakrepro-results.txt). - It runs three scenarios over 30 pages × 3 MB payload:
- Control — page never subscribes to the shared sensor event.
- Leaky — page subscribes, then only
Accelerometer.Stop()on disappear (forgets-=). - Mitigation — page subscribes, then
-=on disappear (the fix).
Observed results (30 pages × 3 MB = 90 MB allocated)
| Target | Control | Mitigation | Leaky (retained after full GC) |
|---|---|---|---|
| Mac Catalyst | 0/30 pages, 0 MB | 0/30 pages, 0 MB | 30/30 pages, 90 MB (100%) |
| Android (API 30 emulator) | 0/30 pages, 0 MB | 0/30 pages, 0 MB | 30/30 pages, 90 MB (100%) |
| iOS 18.x simulator | 0/30 pages, 0 MB | 0/30 pages, 0 MB | 30/30 pages, 90 MB (100%) |
On every target the Leaky run retains the full payload after a forced GC, while Control and Mitigation release everything — proving the unremoved subscription to the static Accelerometer.ReadingChanged is the retention root.
Version with bug
Current main (.NET MAUI 10 / net10.0).
Affected platforms
Android, iOS, Mac Catalyst (cross-platform — the retention is in shared Essentials code). Windows/Tizen share the same pattern.
Did you find any workaround?
Yes — unsubscribe with Accelerometer.ReadingChanged -= handler (the Mitigation scenario), which releases everything. The point of this issue is that the framework does not make the common "forgot to unsubscribe" mistake non-leaking the way Application events already do.
Impact / severity — P3
Apps that use sensors (fitness, AR, games, leveling/measuring tools) typically subscribe per-page and frequently forget -=, because Stop() looks like cleanup. Each leaked page retains its full view-model/payload for the process lifetime; repeated navigation accumulates unboundedly.
Suggested fix
Back the Essentials sensor / Connectivity / Battery static events with WeakEventManager (as Application.RequestedThemeChanged already does), and/or clear subscriber delegates in Stop(). This makes the common mistake non-leaking and consistent with Application events.
Honest scope note
This is partly a usage footgun — the app should call -=. It is filed as a framework-hardening / consistency issue because (a) Application events already use WeakEventManager while these don't, and (b) Stop() strongly implies cleanup but doesn't unsubscribe. It's a reasonable wontfix candidate if the team prefers to keep sensor events strong-referenced.
This issue and its repro project were produced autonomously by GitHub Copilot CLI as a validation of an AI-driven memory-leak detection workflow. All metrics above were measured on real local runs (Mac Catalyst, Android API 30 emulator, iOS 18.x simulator).
Activity
LogishaSelvarajSF4525 commentedon Jun 30, 2026
This issue has been validated in Visual Studio Code 1.126.0 with .NET MAUI main branch. It was reproduced on Android, iOS & Mac platform. I have attached the output below for reference:
Output Screenshot
Fix for memory leak in Accelerometer.ReadingChanged/ShakeDetected by …
Fix for memory leak in Accelerometer.ReadingChanged/ShakeDetected by …
Fix for memory leak in Accelerometer.ReadingChanged/ShakeDetected by …
Fix for memory leak in Accelerometer.ReadingChanged/ShakeDetected by …
Fix for memory leak in Accelerometer.ReadingChanged/ShakeDetected by …
Fix for memory leak in Accelerometer.ReadingChanged/ShakeDetected by …
16 remaining items
Fix for memory leak in Accelerometer.ReadingChanged/ShakeDetected by …
Revert #36503: Weak event handling for Accelerometer events (#38096)
Fix for memory leak in Accelerometer.ReadingChanged/ShakeDetected by …
Fix for memory leak in Accelerometer.ReadingChanged/ShakeDetected by …
Revert #36503: Weak event handling for Accelerometer events (#38096)
Fix for memory leak in Accelerometer.ReadingChanged/ShakeDetected by …
Fix for memory leak in Accelerometer.ReadingChanged/ShakeDetected by …
Revert #36503: Weak event handling for Accelerometer events (#38096)
sheiksyedm commentedon Sep 24, 2026
The fix reverted via this PR #38096. So, reponed this issue.