-
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
Writing callback function pointers leads to unexpected object creation #925
Comments
This sounds like a bug in the override # This patch file was generated by NetBeans IDE
# It uses platform neutral UTF-8 encoding and \n newlines.
--- a/contrib/platform/src/com/sun/jna/platform/win32/OaIdl.java
+++ b/contrib/platform/src/com/sun/jna/platform/win32/OaIdl.java
@@ -607,8 +607,12 @@
@Override
public void read() {
super.read();
+ if(cDims.intValue() > 0) {
rgsabound = (SAFEARRAYBOUND[]) rgsabound[0].toArray(cDims.intValue());
+ } else {
+ rgsabound = new SAFEARRAYBOUND[]{ new SAFEARRAYBOUND() };
}
+ }
@Override
protected List<String> getFieldOrder() { This is a degenerated case - it is a SAFEARRAY without dimensions. |
Hi, I think it's leading towards Structure.java [185]:
As P is not null here, it's native@0x0. Should a zero valued pointer be treated as a null pointer here? |
For the record, your fix solves this problem. I'm just sleuthing to see if this is a more systemic problem in JNA |
@lwahonen could could you please have a look at PR #926? This holds the necessary changeset. For your question and reference in Structure.java, you are in prinicipal right, that for the PLACEHOLDER_MEMORY case p is a C null pointer. The code in |
The safearray fix fixes things, but honestly I'm not familiar enough with the inner workings of Structure.newInstance to comment on that part. Looks good to me, and I think the worst thing that can happen is moving an error from validation time to actual call time. |
Thanks for your input - I merged the changeset to master and the 4.5.X branch. |
lwahonen@35acd04
See above commit for a minimal unit test.
It fails with
java.lang.IllegalArgumentException: Structure field "callback" was declared as interface com.sun.jna.CallbacksTest$vTable$functionpointer, which is not supported within a Structure
Caused by: java.lang.IllegalArgumentException: Exception thrown while instantiating an instance of class com.sun.jna.platform.win32.OaIdl$SAFEARRAY
at com.sun.jna.Structure.newInstance(Structure.java:1790)
at com.sun.jna.Structure.validate(Structure.java:2128)
at com.sun.jna.CallbackReference.getNativeType(CallbackReference.java:275)
at com.sun.jna.CallbackReference.(CallbackReference.java:248)
at com.sun.jna.CallbackReference.getFunctionPointer(CallbackReference.java:449)
at com.sun.jna.CallbackReference.getFunctionPointer(CallbackReference.java:426)
at com.sun.jna.Pointer.setValue(Pointer.java:885)
at com.sun.jna.Structure.writeField(Structure.java:856)
... 20 more
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
at com.sun.jna.Structure.newInstance(Structure.java:1771)
... 27 more
Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
at com.sun.jna.Structure.toArray(Structure.java:1551)
at com.sun.jna.Structure.toArray(Structure.java:1576)
at com.sun.jna.platform.win32.OaIdl$SAFEARRAY.read(OaIdl.java:610)
at com.sun.jna.platform.win32.OaIdl$SAFEARRAY.(OaIdl.java:604)
... 32 more
If you change the parameter type for runtimeId to int, the test passes successfully.
Not sure why writing a function pointer ends up trying to create a SAFEARRAY object. I can patch this by getting the SAFEARRAY as Pointer to the callback, and constructing the SAFEARRAY manually in the callback.
The text was updated successfully, but these errors were encountered: