-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
The free_callback function has a JNI weak global reference leak problem #1586
Comments
Thank you. I think I see the reasoning in the structure. However, could you pleas check the example again? When I run it, I immediately get a segfauls after the first iteration. This happens for JDKs 11, 17 and 21. |
With this additional change: --- a/src/com/sun/jna/CallbackReference.java
+++ b/src/com/sun/jna/CallbackReference.java
@@ -508,7 +508,7 @@
Map<Callback, CallbackReference> map = direct ? directCallbackMap : callbackMap;
synchronized(pointerCallbackMap) {
CallbackReference cbref = map.get(cb);
- if (cbref == null) {
+ if (cbref == null || cbref.cbstruct == null) {
cbref = new CallbackReference(cb, callingConvention, direct);
map.put(cb, cbref);
pointerCallbackMap.put(cbref.getTrampoline(), I can run the sample in a stable manner and reproduce the problem. In my test I see 700MB RSS vs. 385MB RSS with 10.000.000 iterations. One final question @xiezhaokun: Could you check your author information: |
Yes, my user information is all valid. |
The test case I used jna |
I updated the example. |
Fixed via 4bf9f92 |
Version of JNA and related jars
all version
Version and vendor of the java virtual machine
all
Operating system
all
System architecture (CPU type, bitness of the JVM)
Complete description of the problem
The number of arg_classes created by create_callback and the number of arg_classes released by create_callback are inconsistent.
create_callback uses
argc
as the number ofarg_classes
. When(cb->conversion_flags[i] = get_conversion_flag(env, cls)) != CVT_DEFAULT
is true, invoke NewWeakGlobalRef.https://github.com/java-native-access/jna/blob/master/native/callback.c#L154-L163
When
direct
is false , free thecb->arg_classes
, but not free JNI weak reference.https://github.com/java-native-access/jna/blob/master/native/callback.c#L219C3-L224
free_callback uses
cb->cif.nargs
as the number of arg_classes that need to be released, direct is false, cb->arg_classes is NULL. This results in the JNI weak global reference of arg_classes not being released.https://github.com/java-native-access/jna/blob/master/native/callback.c#L329C2-L335C1
Steps to reproduce
native code
java code
The text was updated successfully, but these errors were encountered: