Skip to content

Using @Virtual with method returning @StdString generates function returning dangling reference #374

@equeim

Description

@equeim

For example, for this Java code:

@Namespace("NativeLibrary") @Properties(inherit = org.example.NativeLibraryConfig.class)
public class NativeClass extends Pointer {
        static { Loader.load(); }
        /** Default native constructor. */
        public NativeClass() { super((Pointer)null); allocate(); }
        private native void allocate();

        @Virtual public native @StdString BytePointer get_property();
}

Following C++ class is generated:

class JavaCPP_hidden JavaCPP_NativeLibrary_0003a_0003aNativeClass : public NativeLibrary::NativeClass {
public:
    jobject obj;
    static jmethodID get_1property__;

    JavaCPP_NativeLibrary_0003a_0003aNativeClass() : NativeLibrary::NativeClass(), obj(NULL) { }
    using NativeLibrary::NativeClass::get_property;
    virtual std::basic_string< char >& get_property();
    std::basic_string< char >& super_get_property() { return NativeLibrary::NativeClass::get_property(); }
};

Where JavaCPP_NativeLibrary_0003a_0003aNativeClass::get_property() is:

std::basic_string< char >& JavaCPP_NativeLibrary_0003a_0003aNativeClass::get_property() {
    // Skip boring stuff

    // Dangling reference!
    return StringAdapter< char >(rptr, rsize, rowner);
}

JavaCPP_NativeLibrary_0003a_0003aNativeClass::get_property() should return by value, not reference. It also means that generated override actually overrides nothing, since NativeLibrary::NativeClass::get_property() returns by value.
I suspect that it is caused by StdString.java having @Cast({"std::basic_string", "&"}) annotation, but I'm not sure why it is there.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions