Skip to content

fix(android): keep ArrayBuffer views alive - #2003

Draft
mrousavy wants to merge 1 commit into
mrousavy/kotlin-arraybuffer-wrap-on-deletefrom
mrousavy/kotlin-arraybuffer-safe-views
Draft

mrousavy wants to merge 1 commit into
mrousavy/kotlin-arraybuffer-wrap-on-deletefrom
mrousavy/kotlin-arraybuffer-safe-views

Conversation

@mrousavy

Copy link
Copy Markdown
Member

getBuffer(false) returned a raw direct ByteBuffer with no reference to its owner, so it could outlive the native memory. With close() that is easy to hit.

  • Each getBuffer(false) view now keeps a native reference to the data until the Java GC collects the view. It uses fbjni's DestructorThread.Destructor, a phantom reference, so views are tracked by identity. This also covers ByteBuffer-backed buffers, so wrap(..., onDelete) can't run while a view is still in use.
  • Adds withByteBuffer { } for scoped access without that extra reference, like Swift's withUnsafeBytes.
  • getBuffer(true) now only copies non-owning buffers, like Swift's toData(copyIfNeeded:). Owning buffers, e.g. HardwareBuffer-backed ones, return a view.

A new shared case retains a view (Kotlin getBuffer(false), Swift toData(copyIfNeeded: false), C++ the ArrayBuffer) and checks that the wrapped buffer isn't deleted until the view is released.

Tested on an API 36 emulator: the array-buffers suite passes with this PR. With the view registration removed, the new case fails for Kotlin (deleted while the view still holds the buffer: expected 1 to equal +0).

🤖 Generated with Claude Code

getBuffer(false) used to return a raw view without a reference to its
owner, so it could outlive the native memory. Each view now keeps a
native reference until the Java GC collects it, using fbjni's
DestructorThread as an identity-based phantom reference registry.

Add withByteBuffer { } for scoped access without that extra reference,
like Swift's withUnsafeBytes. getBuffer(true) now only copies non-owning
buffers, like Swift's toData(copyIfNeeded:).
@vercel

vercel Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
nitro-docs Ready Ready Preview Oct 23, 2026 12:37pm UTC

Request Review

@mrousavy
mrousavy added this pull request to stack #2005 October 23, 2026 12:37
@mrousavy
mrousavy removed this pull request from stack #2005 October 23, 2026 12:37
@mrousavy
mrousavy added this pull request to stack #2006 October 23, 2026 12:37
@mrousavy
mrousavy removed this pull request from stack #2006 October 23, 2026 17:52
@mrousavy
mrousavy added this pull request to stack #2007 October 23, 2026 17:52
@mrousavy
mrousavy marked this pull request as draft October 28, 2026 11:10

This branch was successfully deployed

1 active deployment
Preview — 661cc589 Deployed Oct 23, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant