Passing a JS ArrayBuffer to a Java ByteBuffer parameter leaks it in @nativescript/android 9.0.4 through 9.1.1: JsArgConverter wraps the backing store with NewDirectByteBuffer and takes a JNI global ref that it never deletes. The ref keeps the ByteBuffer reachable, so makeInstanceWeakAndCheckIfAlive always answers "alive", the JS finalizer re-arms, and V8 never frees the ArrayBuffer. submitSurfaceFrame passed a fresh copy of each frame this way, so every frame leaked a full surface (576x260, 640x452 or 640x480 bytes) plus one global ref, including frames that Java then discards as unchanged. The draw-list buffer and buzzer payloads leaked the same way. Each payload now goes through a reused Java direct ByteBuffer (app/native/java-direct-buffer.ts): JS writes into it through an ArrayBuffer.from view, and the Java object itself is passed, which takes NativeScript's ordinary object path. Every consumer (SurfaceCompositor applyAndComposite, playBuzzerSequence) copies before returning, so one buffer per JS context is safe. Covered: the communicator bridge (frames, draw lists, buzzer, which also carries EvenHub tones), the preview-only display, and all eight worker apps. Workers submit through the Android getActiveDisplay() (frames) and worker-buzzer (tones), so the reused buffers live there rather than in each worker; the worker sources are shared with iOS, whose active display takes the ArrayBuffer itself. The smaller one-off crossings (glyph and image atlas registration, the gray preview helper, firmware and file writes) are left as they are. tests/java-direct-buffer.test.cjs covers the helper against a fake ByteBuffer, and the Android worker-buzzer test stubs it. (Rebased onto main by James Babcock: worker changes moved into active-display.ts and worker-buzzer.ts.) Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
55 lines
1.8 KiB
TypeScript
55 lines
1.8 KiB
TypeScript
import { JavaDirectBuffer } from "./java-direct-buffer";
|
|
|
|
declare const com: any;
|
|
|
|
// Reused Java-side buffers for this isolate's frames: passing a JS ArrayBuffer
|
|
// to Java leaks it (java-direct-buffer.ts). Module state is per isolate, so
|
|
// each worker gets its own pair.
|
|
const framePixelsBuffer = new JavaDirectBuffer(640 * 480);
|
|
const frameDrawsBuffer = new JavaDirectBuffer();
|
|
|
|
/**
|
|
* The display a worker isolate submits surface frames to: the live BLE
|
|
* communicator, or the preview-only compositor standing in when no glasses
|
|
* are paired (both expose the same submitSurfaceFrame signature). Located
|
|
* through Java statics because those are shared across isolates, unlike any
|
|
* JS-side state. Null when neither exists.
|
|
*
|
|
* Frame submission only. Buzzer effects use worker-buzzer, which routes to
|
|
* the platform BLE session and skips playback when disconnected.
|
|
*/
|
|
export function getActiveDisplay(): typeof display | null {
|
|
return activeJavaDisplay() ? display : null;
|
|
}
|
|
|
|
function activeJavaDisplay(): any {
|
|
return (
|
|
com.faceclaw.app.FaceclawBleCommunicator.getActive() ??
|
|
com.faceclaw.app.FaceclawPreviewCompositor.getActive()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Takes the same ArrayBuffer arguments as the iOS active display and copies
|
|
* them into the reused Java buffers right before the synchronous Java call.
|
|
*/
|
|
const display = {
|
|
submitSurfaceFrame(buffer: ArrayBuffer, surfaceId: string, x: number, y: number, width: number, height: number,
|
|
fingerprint: string, paintMs: number, frameId: number, draws: ArrayBuffer | null = null): void {
|
|
const target = activeJavaDisplay();
|
|
if (!target) return;
|
|
target.submitSurfaceFrame(
|
|
framePixelsBuffer.load(new Uint8Array(buffer)),
|
|
surfaceId,
|
|
x,
|
|
y,
|
|
width,
|
|
height,
|
|
fingerprint,
|
|
paintMs,
|
|
frameId,
|
|
frameDrawsBuffer.loadOptional(draws),
|
|
);
|
|
},
|
|
};
|