Question about std::shared_ptr callback and Pointer casting in JavaCPP

18 views
Skip to first unread message

y z (s1mple)

unread,
Aug 26, 2025, 4:28:41 AM (12 days ago) Aug 26
to javacpp

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 { 
    std::string my_string; 
}; 

template <typename M0> using CallbackFunc = std::function<void(std::shared_ptr<M0>)>; 

void callback(CallbackFunc<MyString> func) { 
    std::shared_ptr<MyString> p = std::make_shared<MyString>(); 
    p->my_string = "hello"; 
    func(p); 
    return; 
}

JavaCPP Configuration:

// I'm mapping CallbackFunc to FunctionPointer 
public static class CallbackFunc extends FunctionPointer { 
    static { Loader.load(); } 
    public CallbackFunc(Pointer p) { super(p); } 
    protected CallbackFunc() { allocate(); } 
    private native void allocate(); 
    public native void call(@ByVal @Cast("std::shared_ptr<MyString>*") Pointer string); 
}

Java Test Code:

CallbackFunc cb = new CallbackFunc() { 
    public void call(Pointer str) { 
        MyString str1 = new MyString(str); 
        String str2 = str1.my_string().getString(); 
        System.out.println(str2); 
    } 
};
callback(cb);

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.

Samuel Audet

unread,
Aug 26, 2025, 4:48:23 AM (12 days ago) Aug 26
to javacpp...@googlegroups.com

y z

unread,
Aug 26, 2025, 5:26:40 AM (12 days ago) Aug 26
to javacpp...@googlegroups.com

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.


Samuel Audet <samuel...@gmail.com> 于2025年8月26日周二 16:48写道:
--
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.
Reply all
Reply to author
Forward
0 new messages