Skip to content

Free native allocations when ByteBuffer creation fails - #3

Merged
riccardobl merged 1 commit into
NostrGameEngine:masterfrom
toaster0123:fix/allocation-wrapper-oom-20261005
Oct 5, 2026
Merged

riccardobl merged 1 commit into
NostrGameEngine:masterfrom
toaster0123:fix/allocation-wrapper-oom-20261005

Conversation

@toaster0123

Copy link
Copy Markdown
Contributor

Problem

malloc, calloc, and mallocAligned allocate native memory before creating the Java ByteBuffer. If the JVM cannot allocate that wrapper, the method throws without returning an address or releasing the native allocation.

Change

Release newly allocated memory when wrapper creation throws or returns null. Leave the raw native API and existing realloc behavior unchanged; realloc exception safety is handled separately.

Regression evidence

On the original master (6439892), each API leaked 4096 bytes when wrapper creation failed in an isolated JVM with a 16 MiB heap. The new forked regression verifies that the native allocated-byte counter returns to its original value for all three APIs. It fails against the original code and passes with this fix.

Validation

  • Focused JUnit allocation-failure regression passed for malloc, calloc, and aligned allocation
  • Linux x86_64 JNI built directly with GCC and pinned mimalloc v2.2.7, MI_SECURE=4
  • Ordinary allocation, reads/writes, realloc growth/shrink, calloc zeroing, alignment, free, and accounting checks passed
  • Java sources compiled with source/target 8; git diff --check passed

Limits

The complete Gradle/CMake build and repository-wide suite were not run in this cloud environment. The installed Java runtime lacks --release 8 platform data. Windows, macOS, Android, iOS, and ARM runtime behavior was not tested. The bounded heap-pressure test uses a separate JVM and leaves the main test runner's heap untouched.

@riccardobl
riccardobl marked this pull request as ready for review October 5, 2026 20:05
@riccardobl
riccardobl merged commit 5b6f78f into NostrGameEngine:master Oct 5, 2026
9 checks passed
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.

2 participants