Hi,
Am Mittwoch, dem 26.01.2022 um 10:42 -0800 schrieb Kyle Hayes:
> [Store object ref in native code for callback]
>
> While I can see how to pass a Java object reference (it looks like it
> gets passed as a long, i.e. 64-bit on 64-bit platforms), what I am
> not sure about is what happens when I store that on the native side
> for long periods of time. Does JNA make sure that the GC does not
> move the object? If not, is there a way to pin it or otherwise make
> sure that the GC does not move it around?
No it does not. In fact, with out specifying
Library#OPTION_ALLOW_OPTIONS, no java object will reach native code.
Even if you activate that you are not done yet. If you want to use the
references beyond the method call, you'd need to add a global reference
using JNI calls on the native side. I suggest a different approach:
You already need a mechanism to keep your callbacks alive on Java side,
so why not add a field to the callback object and hold the data there.
An alternative (if callback and data object are not related) is a map
and passing the key to native code (beware: if you use string keys, you
need to copy on the native side).
Maybe this helps?!
Greetings
Matthias