Repository navigation
fix(SafeArea): subscribe to process-global InputPane/ApplicationView events weakly - #1603
Conversation
…events weakly SafeAreaDetails subscribed to the process-global ApplicationView.VisibleBoundsChanged and InputPane.Showing/Hiding events with instance-method delegates, so the singleton strongly rooted the SafeAreaDetails and (through its subscription closures) the owner element. Unregister() undoes this on the owner's Unloaded event, but that event does not fire when the owner's AssemblyLoadContext is torn down abruptly (e.g. a host unloading a collectible plugin/preview ALC), leaking the owner and its ALC for the process lifetime — and accumulating one leak per unloaded owner. Subscribe via a weak wrapper (CreateWeakHandler) that holds only a WeakReference to the SafeAreaDetails and self-detaches once it is collected, so the global singleton can no longer pin it. Behaviour is unchanged while the owner is alive. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
There was a problem hiding this comment.
Pull request overview
This PR addresses a memory leak in SafeAreaDetails where subscribing with instance delegates to process-global ApplicationView / InputPane events could strongly root discarded UI trees (and collectible AssemblyLoadContexts) for the lifetime of the process. The fix introduces weak event-handler wrappers so global singletons can no longer keep SafeAreaDetails (and thus its owner subtree) alive once the owner is discarded.
Changes:
- Added a
CreateWeakHandler<TSender, TArgs>helper to createTypedEventHandlerdelegates that only retain aWeakReference<SafeAreaDetails>and self-detach once the target is collected. - Updated
RegisterEvents()to subscribe toApplicationView.VisibleBoundsChangedandInputPane.Showing/Hidingvia weak handlers while preserving eager-unsubscribe behavior.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Azure Static Web Apps: Your stage site is ready! Visit it here: https://polite-field-01aa64f0f-1603.eastus2.azurestaticapps.net |
Addresses review feedback (#1602): the weak-handler guarantee is defeated if a caller passes a capturing lambda (its closure would be rooted by the delegate, reintroducing a strong reference). Add DEBUG assertions that onEvent/detach have a null Target (static/non-capturing) to catch accidental captures during development. Co-Authored-By: Claude Fable 5 <[email protected]>
|
Azure Static Web Apps: Your stage site is ready! Visit it here: https://polite-field-01aa64f0f-1603.eastus2.azurestaticapps.net |
Co-Authored-By: Claude Fable 5 <[email protected]>
|
Azure Static Web Apps: Your stage site is ready! Visit it here: https://polite-field-01aa64f0f-1603.eastus2.azurestaticapps.net |
…CS0119) On net*-android, an unqualified 'Debug' binds to the inherited Android.Views.ViewGroup.Debug(int) member rather than System.Diagnostics.Debug, so 'using System.Diagnostics; Debug.Assert(...)' fails to compile with CS0119. Fully-qualify System.Diagnostics.Debug.Assert (and drop the now-unused using). Verified: the failing CI job's only compile error was this CS0119; net9.0 builds clean. Co-Authored-By: Claude Fable 5 <[email protected]>
|
Azure Static Web Apps: Your stage site is ready! Visit it here: https://polite-field-01aa64f0f-1603.eastus2.azurestaticapps.net |
CreateWeakHandler returned the wrapper delegate AND assigned the same value to an out parameter. Both call sites can capture the return value in a local (the InputPane site already did, discarding `out _`), so the out parameter was pure redundancy. Callers now capture the returned handler and use it for both the event subscription and the unsubscribe closure. No behaviour change: the delegate's captured state (a WeakReference only) is unaffected by how the caller stores the returned reference. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Azure Static Web Apps: Your stage site is ready! Visit it here: https://polite-field-01aa64f0f-1603.eastus2.azurestaticapps.net |
ALC pin-fix — standalone (uno.toolkit.ui). Base:
main. Independent of the uno framework stack; a downstream host-side sweep mitigates until this ships.Fixes #1602
Spec
SafeAreaDetailssubscribed to the process-globalApplicationView.VisibleBoundsChangedandInputPane.Showing/Hidingevents with instance-method delegates, so the process-lifetime singleton strongly rooted theSafeAreaDetailsand — through its_subscriptionsclosures, which captureowner— the owner element and its whole subtree.Unregister()undoes this only on the owner'sUnloaded, which does not fire when a tree is discarded abruptly (e.g. a host unloading a plugin/preview app hosted in a collectibleAssemblyLoadContext), leaking the owner — and its ALC — for the process lifetime, one leak per unloaded owner.This PR makes the global-event subscriptions weak so the singleton can never root a discarded
SafeAreaDetails, while keeping behaviour identical while the owner is alive. Verified downstream in a designer-style host via ClrMD heap-dump root attribution: after this change (as part of a broader pin audit), zero objects from an unloaded preview-app ALC remain reachable and the ALC completes unloading.Implementation
src/Uno.Toolkit.UI/Controls/SafeArea/SafeArea.cs(+56/−6, single file):SafeAreaDetails.CreateWeakHandler<TSender, TArgs>(target, onEvent, detach, out handler): builds aTypedEventHandler<TSender, TArgs>whose closure captures only aWeakReference<SafeAreaDetails>. While the target is alive it forwards toonEvent; once the target has been collected it self-detaches from the sender viadetach. Both callbacks are required to bestatic(non-capturing) lambdas so the returned delegate never captures the target instance; the handler is also returned viaoutso the normalUnloadedpath can still unsubscribe eagerly.RegisterEvents()—ApplicationView: subscribesVisibleBoundsChangedthroughCreateWeakHandlerforwarding toOnInsetUpdateRequired; the eager-unsubscribe closure added to_subscriptionsnow captures the sameappViewinstance instead of re-callingGetForCurrentView()at dispose time.RegisterEvents()—InputPane: one weak wrapper serves bothShowingandHiding(forwarding toOnInputPaneChanged); itsdetachremoves it from both events; the eager_subscriptionsunsubscribe is preserved.SizeChangedsubscription is intentionally left as a strong instance delegate: the scroll ancestor belongs to the same visual tree as the owner, so it cannot outlive it — there is no cross-lifetime root there.RegisterEvents()documents why weak subscription is mandatory for process-global sources.Test plan
SafeAreaTests.When_Owner_Discarded_Without_Unloaded_Details_And_Owner_Are_Collectedinsrc/Uno.Toolkit.RuntimeTests/Tests/SafeAreaTests.cs. Arrange: load aSafeArea-annotatedGrid, resolve itsSafeAreaDetailsvia the existing internalFindInstancehook, detach the privateOnOwnerUnloadedhandler (reflection-bound delegate) to model teardown withoutUnloaded, clear content inside a[NoInlining]helper, keep onlyWeakReferences. Act: GC collect/finalize loop with dispatcher idles. Assert: bothWeakReferences die. Fails on main (singletons root the graph); passes with this change.SafeAreaTestsruntime tests cover the alive-path behaviour (insets still applied onVisibleBoundsChanged/ input-pane transitions) — unchanged, since the weak wrapper forwards to the sameOnInsetUpdateRequired/OnInputPaneChangedmethods.ApplicationView/InputPane-rooted pins are gone and the ALC unloads to zero retained objects.Risks / rollback
WeakReference) stays on the singleton until the next event firing self-detaches it, or until the eagerUnloaded-path unsubscribe ran. It cannot root the owner tree; cost is a few dozen bytes per collected instance, bounded and self-cleaning.SafeArea.cs.