Skip to content

fix(android): hold the SegmentedBar from its tab content factory weakly - #11657

Merged
edusperoni merged 1 commit into
mainfrom
fix/android-segmented-bar-factory-cycle
Nov 22, 2026
Merged

edusperoni merged 1 commit into
mainfrom
fix/android-segmented-bar-factory-cycle

Conversation

@edusperoni

Copy link
Copy Markdown
Contributor

PR Checklist

What is the current behavior?

SegmentedBar keeps its TabContentFactoryImpl in _tabContentFactory, and the factory kept the bar as a strong owner. disposeNativeView clears the tab-change listener's owner but never the factory's:

TabContentFactoryImpl --(JS) owner--> SegmentedBar --_tabContentFactory--> TabContentFactoryImpl

The cycle runs through the factory's native counterpart (Java also holds the factory in each TabSpec). With the default wrapper lifetime, the runtime breaks it once the factory's wrapper is unreachable from JS. A wrapper lifetime that keeps a JS-implemented object's state for as long as Java holds the object (the experimental canary mode, NativeScript/android#2127) cannot, so every SegmentedBar stays alive, along with everything it references. That is 18 bars on the automated suite, even when disposed normally.

What is the new behavior?

The factory holds the bar through a WeakRef, like TabView's PagerAdapter. If the bar is gone when Android asks for tab content, it returns the same collapsed TextView, built on the application context. The factory itself is still held by the bar and by Java, as before, and the bar is held by its parent, so nothing it needs becomes weaker under resurrection.

New SEGMENTED-BAR spec test_SelectingTabsAfterGarbageCollection_CreatesTheirContent collects both heaps between tab selections, which makes TabHost create tab content lazily through the factory.

Verified on an Android 15 (API 35) emulator against an Android runtime built from NativeScript/android#2130 (stacked on #2127), with this change and the other Android wrapper-lifetime fixes applied together:

  • "wrapperLifetime": { "mode": "resurrecting" } (today's default behavior, confirmed with getConfig("wrapperLifetimeMode")): full automated suite 1703 OK, 0 failed. The runtime counted 78,980 resurrections and 29,187 collected wrappers over the run, so the new specs ran while wrappers were being resurrected and their Java objects released.
  • "wrapperLifetime": { "mode": "canary", "scope": "all" }: full suite 1703 OK, 0 failed. After the run, findLeakCycles() reports 0 cycles, 0 leaked objects, compared with 1054 leaked objects on main.

@nx-cloud

nx-cloud Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit b094934

Command Status Duration Result
nx run-many -t build -p core,webpack5,vite ✅ Succeeded 23s View ↗

💡 With Ultracache enabled, Nx Cloud learns each task's file inputs in CI and uses them on later runs. Turn on Ultracache in workspace settings ↗ · Read docs ↗


☁️ Nx Cloud last updated this comment at 2026-11-22 18:56:34 UTC

@pkg-pr-new

pkg-pr-new Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@nativescript/core@11657
npm i https://pkg.pr.new/@nativescript/vite@11657
npm i https://pkg.pr.new/@nativescript/webpack@11657

commit: b094934

@edusperoni
edusperoni marked this pull request as ready for review November 20, 2026 21:45

@NathanWalker NathanWalker left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Holding SegmentedBar via WeakRef in TabContentFactoryImpl with the androidUtils.getApplicationContext() fallback in createTabContent cleanly breaks the _tabContentFactory <-> owner cycle while preserving lazy tab creation across GC.

@NathanWalker NathanWalker added this to the 9.2 milestone Nov 21, 2026
SegmentedBar keeps its TabContentFactoryImpl in _tabContentFactory, and
the factory kept the bar in a strong owner property, which
disposeNativeView never cleared. Wrapper resurrection used to break
this cycle; a runtime that keeps a JS-implemented object's state for as
long as Java holds the object cannot, so every SegmentedBar and its view
subtree stayed alive. The factory now holds the bar through a WeakRef
and falls back to the application context once the bar is gone.
@edusperoni
edusperoni force-pushed the fix/android-segmented-bar-factory-cycle branch from 90ff841 to b094934 Compare November 22, 2026 18:53
@edusperoni
edusperoni merged commit a383359 into main Nov 22, 2026
10 checks passed
@edusperoni
edusperoni deleted the fix/android-segmented-bar-factory-cycle branch November 22, 2026 19:14
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.

3 participants