Hello,
I'm using JavaCPP to create a native interface for a C++ library. I am encountering an issue when handling a callback that uses std::shared_ptr as a parameter.
The problem is that the Pointer received in the Java callback cannot be correctly cast to the generated MyString type, resulting in a NullPointerException.
Here is the relevant code and the error message:
C++ side:
class MyString {JavaCPP Configuration:
// I'm mapping CallbackFunc to FunctionPointerJava Test Code:
CallbackFunc cb = new CallbackFunc() {Execution Result:
An exception occurred while executing the Java class. java.lang.NullPointerException: Cannot invoke "MyString.my_string()" because the return value of "MyString.my_string()" is null
My analysis:
I've found that if the C++ callback parameter is not wrapped in std::shared_ptr (e.g., MyString*), the conversion works correctly. The issue seems to be with how JavaCPP handles the std::shared_ptr object itself, possibly casting the Pointer to the smart pointer object's address instead of the managed MyString object's address.
What is the correct way to handle std::shared_ptr as a callback parameter in JavaCPP?
Thank you for your time.
Thank you so much for your help and excellent advice.
I followed your guidance and correctly implemented the @SharedPtr
annotation, and it completely solved the NullPointerException
issue.
Your explanation was very clear and helped me understand the correct way to handle std::shared_ptr
in JavaCPP. I truly appreciate your support.
--
You received this message because you are subscribed to the Google Groups "javacpp" group.
To unsubscribe from this group and stop receiving emails from it, send an email to javacpp-proje...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/javacpp-project/b8aab55d-8091-401e-b852-a0d35ddad0b0%40gmail.com.