Skip to content

Essentials sensor static events (Accelerometer.ReadingChanged et al.) leak subscribers — no WeakEventManager, Stop() doesn't unsubscribe #36216

Open

Description

@kubaflo

🔍 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 of Start() / 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.

  1. Build & run on any target — the harness auto-runs on launch and prints results bracketed by >>>SENSOR_LEAK_REPRO>>> (and writes sensorleakrepro-results.txt).
  2. 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

  1. added
    area-essentialsEssentials: Device, Display, Connectivity, Secure Storage, Sensors, App Info
    on Jun 29, 2026
  2. LogishaSelvarajSF4525 commented on Jun 30, 2026

    @LogishaSelvarajSF4525
    Contributor

    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

    In iOS In Android In Mac
    Image Image Image
  3. added this to the .NET 10 SR10 milestone on Jun 30, 2026
  4. 16 remaining items

  5. sheiksyedm commented on Sep 24, 2026

    @sheiksyedm
    Contributor

    The fix reverted via this PR #38096. So, reponed this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-essentialsEssentials: Device, Display, Connectivity, Secure Storage, Sensors, App Infopartner/syncfusionIssues / PR's with Syncfusion collaborationplatform/androidplatform/iosplatform/macosmacOS / Mac Catalysts/triagedIssue has been revieweds/verifiedVerified / Reproducible Issue ready for Engineering Triage

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions