GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — ByteArrayProxy data corruption due to pointer getting GC'd? · Issue #252 · lmdbjava/lmdbjava

ByteArrayProxy data corruption due to pointer getting GC'd? #252

New issue
Copy link
New issue
Copy link
Closed
Closed
ByteArrayProxy data corruption due to pointer getting GC'd?#252
Copy link
Assignees
bp-alex
Labels
bugAn issue relates to behaviour that violates the intention of the libraryAn issue relates to behaviour that violates the intention of the library

Description

@oti-dev
oti-dev
opened on Mar 27, 2025
Issue body actions

Hey, I've encountered an issue similar to this one: #207 . One key difference is that I use ByteArrayProxy instead of any direct buffers.

I'm using the newest lmdbjava along with lmdb binary built from newest version (commit).

So I've been following the steps from the mentioned issue and it's true that adding System.gc() call right before mdb_put makes the bug more frequent. I've been looking at the implementation of the ByteArrayProxy and one thing caught my attention. Here's the code from it:

  @Override
  protected void in(final byte[] buffer, final Pointer ptr, final long ptrAddr) {
    final Pointer pointer = MEM_MGR.allocateDirect(buffer.length);
    pointer.put(0, buffer, 0, buffer.length);
    ptr.putLong(STRUCT_FIELD_OFFSET_SIZE, buffer.length);
    ptr.putAddress(STRUCT_FIELD_OFFSET_DATA, pointer.address());
  }

What worries me is that the pointer's underlying memory lifespan is actually longer than the pointer reference itself. From my understanding of how TransientNativeMemory in jnr-ffi works, once a Pointer has no reference to it, it's subject to be cleared by the "cleaner" thread. So there's a slim chance of mdb_put operating on memory that has been cleared already. Am I correct?

What I'm currently testing is adding a static list of references to pointers inside the proxy, and then manually clearing it once transaction is done: bdsystems@9e5eee9
So far this seems to work correctly, but with these kind of issues testing has to take time.

What do you think? Could that be the bug, or is my hypothesis wrong? I'm new to java memory management and jnr-ffi in particular so please correct me if my reasoning is faulty.

Activity

  1. bp-alex
    closed this as completedin c91893aon Oct 25, 2025
  2. bp-alex
    added
    bugAn issue relates to behaviour that violates the intention of the libraryAn issue relates to behaviour that violates the intention of the library
    on Oct 25, 2025
  3. bp-alex
    self-assigned this
    on Oct 25, 2025
  4. bp-alex commented on Oct 25, 2025

    @bp-alex
    bp-alex
    on Oct 25, 2025
    Member
    More actions

    Well spotted @oti-dev. I note a few issues in JNR-FFI report something similar, eg jnr/jnr-ffi#171. The solution as you note is to maintain a reference. I think this wasn't detected earlier because so few people use the ByteArrayProxy and it depends on the precise timing of the Magazine finalization setup by https://github.com/jnr/jnr-ffi/blob/master/src/main/java/jnr/ffi/provider/jffi/TransientNativeMemory.java. In any event changes in commit c91893a should resolve this properly.

    Reacted by Dominik Otkiński
  5. at055612
    added a commit that references this issue on Oct 26, 2025

    Fix transient pointer deallocation in ByteArrayProxy

    This issue will close once commit c91893a is merged into the 'master' branch.
    c91893a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @bp-alex
    bp-alex

Labels

bugAn issue relates to behaviour that violates the intention of the libraryAn issue relates to behaviour that violates the intention of the library

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.