I am writing some Rust code that uses a Rust library that doesn't seem to have a corresponding Java library (didcomm-rs).
Based on examples I've found, to pass String values between Rust and Java, I can use String
in Java, and *const c_char
in Rust.
And this seems to work fine, except that I'm running into something strange now.
I have a unit test in Java to test my code and on the last call into Rust it crashes with the following:
The forked VM terminated without properly saying goodbye. VM crash or System.exit called?
I have put println!
statements in the Rust code and I can see in the resulting dump file (where these println statements go) that the code does reach the end.
This is the last few lines of code in my Rust library:
// other code
let result = to_ptr(base64::encode(&message));
println!("6");
result
}
fn to_ptr(string: String) -> *const c_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
}
I suspect it might be related to either the Rust code or the Java code cleaning up a variable or something like that, but I'm not sure how to debug this.