Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BytePointer creates leak #380

Closed
nickbernz opened this issue Feb 20, 2017 · 7 comments
Closed

BytePointer creates leak #380

nickbernz opened this issue Feb 20, 2017 · 7 comments
Labels

Comments

@nickbernz
Copy link

In BytePointer::BytePointer(ByteBuffer buffer), hasArray() is used to check whether or not buffer is direct. But since it is possible for a direct buffer to return true on that method, this leads to a memory leak.

public BytePointer(ByteBuffer buffer) {
        super(buffer);
        if (buffer != null && buffer.hasArray()) {
            byte[] array = buffer.array();
            allocateArray(array.length - buffer.arrayOffset());
            put(array, buffer.arrayOffset(), array.length - buffer.arrayOffset());
            position(buffer.position());
            limit(buffer.limit());
        }
    }

I would suggest to using instead is isDirect().

public BytePointer(ByteBuffer buffer) {
        super(buffer);
        if (buffer != null && !buffer.isDirect()) {
            byte[] array = buffer.array();
            allocateArray(array.length - buffer.arrayOffset());
            put(array, buffer.arrayOffset(), array.length - buffer.arrayOffset());
            position(buffer.position());
            limit(buffer.limit());
        }
    }
@saudet
Copy link
Member

saudet commented Feb 21, 2017

Do you have an example of a direct buffer with an array?

There can also be non-direct buffers without array, so it's not safe to call array() unless hasArray() returns true...

@nickbernz
Copy link
Author

nickbernz commented Feb 21, 2017

On my Pixel, the following creates direct buffers with array.

ByteBuffer buffer = ByteBuffer.allocateDirect((1024*1024));
boolean hasArray = buffer.hasArray();
boolean isDirect = buffer.isDirect();

Maybe you could check both conditions?

if (buffer != null && !buffer.isDirect() && buffer.hasArray()) {

@saudet
Copy link
Member

saudet commented Feb 21, 2017 via email

@nickbernz nickbernz reopened this Feb 21, 2017
@nickbernz
Copy link
Author

I am sorry, I am not sure what you are asking for.

@saudet
Copy link
Member

saudet commented Feb 21, 2017 via email

@saudet saudet added the bug label Mar 4, 2017
@saudet
Copy link
Member

saudet commented Mar 4, 2017

Please do send a patch to fix this issue! Thanks a lot

saudet added a commit to bytedeco/javacpp that referenced this issue Mar 8, 2017
@saudet
Copy link
Member

saudet commented Mar 12, 2017

I've included in JavaCPP 1.3.2 the changes you proposed. Thanks for reporting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants