bytedeco / javacpp

The missing bridge between Java and native C++
Other
4.49k stars 583 forks source link

Pointer's nio Buffer initializer doesn't work as expected #526

Closed oneengineer closed 2 years ago

oneengineer commented 2 years ago

I want to initialize a pointer with data store in a ByteBuffer. However the pointer seems to be created as size of 1 byte. Here is what I did:

      val nioBuffer = ByteBuffer.wrap(data.get)
      val hostData = new Pointer( nioBuffer )  

I look up the initializer of the Pointer, it seems the code never goes to !isNull() because the address is initialized as 0. So that address/position/capacity won't be set as the buffers'.

    public Pointer(final Buffer b) {
        if (b != null) {
            allocate(b);
        }
        if (!isNull()) {
            address -= b.position() * sizeof();
            position = b.position();
            limit = b.limit();
            capacity = b.capacity();
            deallocator = new ProxyDeallocator(this, b);
        }
    }
oneengineer commented 2 years ago

I just checked BytePointer works well, not sure the purpose of Pointer's Buffer constructor.

saudet commented 2 years ago

It's only going to do something for direct buffers. There's no void[], or VoidBuffer, so there's nothing we can do there. I think the docs are pretty clear: http://bytedeco.org/javacpp/apidocs/org/bytedeco/javacpp/Pointer.html#Pointer-java.nio.Buffer-

saudet commented 2 years ago

If you would like to make it clearer though, please open a pull request with your suggestions. Thanks!

oneengineer commented 2 years ago

My bad, I don't what direct refer at the first place. The docs are very clear. Thanks!