You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
o/ I'm migrating from JNA to JNR, and almost everything works fine. However, I've run into a really odd bug when using callback functions. I've built a minimal project that reproduces it. The JNA equivilent works fine.
importjnr.ffi.LibraryLoader;
importjnr.ffi.annotations.Delegate;
publicclassMain {
publicinterfaceNatives {
voidsimple_callback(SimpleCallbackcallback);
interfaceSimpleCallback {
@Delegatevoidinvoke(Stringvalue);
}
staticNativesload() {
returnLibraryLoader
.create(Natives.class)
.load("<path to the library>");
}
}
publicstaticvoidmain(String[] args) {
Nativesnatives = Natives.load();
for (inti = 0; i < 10; i++) {
natives.simple_callback(System.out::println);
}
}
}
Rust:
use std::ffi::{c_char,CString};use std::mem;/// Converts a Rust string to a Java stringpubfnto_java_string(string:&str) -> *constc_char{let cs = CString::new(string.as_bytes()).unwrap();let ptr = cs.as_ptr();// Tell Rust not to clean up the string while we still have a pointer to it.// Otherwise, we'll get a segfault.
mem::forget(cs);
ptr
}#[no_mangle]externfnsimple_callback(callback:externfn(*constc_char)){let value = "Any string value";callback(to_java_string(&value));}
The output:
Any string value
Any string value
Any string value lG�|� ���� $�? � dRTypeCache � |� ���� %�� �|�
Any string value
Any string value
��hNG�|� �,0�|�
Any string value
Any string value |��|� A���|���
Any string value
Any string value
(The corruption is different every time)
Any idea what could be causing this?
The text was updated successfully, but these errors were encountered:
I assume the conversion is done by StringResultConverter. Looking at it, it seems Java default charset is used to determine the width of the string termination.
It is just a guess, but maybe your default charset results in a width > 1.
o/ I'm migrating from JNA to JNR, and almost everything works fine. However, I've run into a really odd bug when using callback functions. I've built a minimal project that reproduces it. The JNA equivilent works fine.
Note: My native library is written in Rust
How to reproduce
Java:
Rust:
The output:
(The corruption is different every time)
Any idea what could be causing this?
The text was updated successfully, but these errors were encountered: