-
Notifications
You must be signed in to change notification settings - Fork 588
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
Invalid free for methods that return array with @Const and @Adapter annotations #317
Comments
I've made changes that fixes this in the latest commit. Please give it a try, and thanks for reporting! |
Yep, it fixed this for me. Although I still don't really understand why we need to perform additional allocation and subsequent deletion of char* buffer if get_stdstring() returns string by value or non-const reference. |
It's just how it's assuming the adapter works, which is useful when returned as a |
But in this case it doesn't return a Pointer, it returns an array. Only thing that is done with the pointer returned from adapter is SetByteArrayRegion() which doesn't take ownership over it, it does a deep copy. Then the pointer is deleted if it was non-const. |
Yes, I got that, that's fine. Like I said, you're more than welcome to work on that. |
The bug is fixed in JavaCPP 1.5.1, but please do send a pull request with your additional enhancement when you have the chance! Thanks for your help |
… Java arrays from adapters (issue #317)
Actually, I think commit 55e3bdc solves the second issue you're describing. With these changes, I'm pretty sure that the deallocator will only be needed and used in the case of non- |
I.e. suppose we have this native function
On Java side we will have this method:
And generated JNI code will be:
The problem is that rptr is
const signed char*
due to@Const
annotation and StringAdapter's conversion operator doesn't allocate memory when returning const pointer. Therefore, when we will calldeallocator
, process will be terminated due to invalid free. I suppose that this code should check if method has@Const
annotation and call deallocator only if it hasn't. Or better, just always declarerptr
as const, and never call deallocator.rptr
is immediately passed to SetByteArrayRegion() which does its own copy and that's that. There is no need forrptr
not to be const.The text was updated successfully, but these errors were encountered: