Skip to content

fix(android): don't hold the collectionview strongly from listeners given to native code - #117

Merged
farfromrefug merged 1 commit into
masterfrom
fix/android-weakref-listener-closures
Nov 5, 2026
Merged

farfromrefug merged 1 commit into
masterfrom
fix/android-weakref-listener-closures

Conversation

@farfromrefug

Copy link
Copy Markdown
Member

Summary

The listeners the collectionview gives to native code closed over this (or the cell holder). A JS closure given to a native object lives as long as that object, so the collectionview, its holders and their views kept each other alive. With the napi runtimes (QuickJS-ng) the JS proxies are then never finalized: every collectionview that was shown stayed in memory with its recyclerview, cells and what they hold.

Changes

  • index.android.ts: capture the collectionview through a WeakRef in the closures given to native code: view pool, recycler, adapter interface (bind(this) x6), scroll, item touch, size changed, layout completed, pre-draw, item overlap decoration.
  • The per-cell click listener captures the holder and the collectionview through WeakRef too.
  • Holders stay held by _viewHolders, so the values stored on them (view, position, defaultItemView) live as long as the collectionview does.

Verification

  • tsc --noEmit -p packages/collectionview/tsconfig.json and prettier pass.
  • Run in an app on the Android emulator (QuickJS-ng runtime, forced GC after each cycle, same patch applied to the compiled package): open/close a page with a list, a page on top of it, and a settings page.
    • Views retained per cycle: +4 to 0 (list page), +10 to 0 (list then another page), +30 to +12 (settings; the rest is not from the collectionview).
    • Heap dump after the cycles: CollectionViewCellHolder 100 to 0, RecyclerView 20 to 0.
  • Not checked: iOS (not touched), the vue/angular/react flavors (they use this same android implementation), all scroll/reorder/overlap code paths. The listeners return a default (0, -1, false) if the collectionview is already collected, which should not happen while its native view is attached.

Notes

🤖 Generated with Claude Code

…iven to native code

The pool, recycler, adapter, scroll, touch, size, layout and pre-draw listeners, and the
click listener of each cell, closed over `this` (or the cell holder) with arrow functions or
`bind(this)`. A JS closure given to a native object stays alive as long as that object does,
so the collectionview, its holders and their views kept each other alive: with the napi
runtimes (QuickJS-ng) the JS proxies are then never finalized and every collectionview that
was shown stays in memory, with its recyclerview, cells and what they hold.

Capture the collectionview and the holder through a WeakRef instead. The holders stay held by
`_viewHolders`, which keeps the values stored on them (view, position) alive while the
collectionview is.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@farfromrefug
farfromrefug marked this pull request as ready for review November 5, 2026 14:56
@farfromrefug
farfromrefug merged commit da3fb1e into master Nov 5, 2026
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.

1 participant