I have the following C++ method compiled using Visual Studio 2017:
extern "C" __declspec( dllexport )
Info* __stdcall GetInfo(InfoProvider* infoProvider)
{
static_assert(std::is_pod<Info>::value, "Must be Plain Old Data in order to be safely copied between DLL boundaries");
Info info = new Info();
Info->data1 = infoProvider->data1;
Info->data2 = infoProvider->data2;
return info;
}
In Java code, it is mapped by Java Native Runtime using interface method with following signature:
Info GetInfo(Pointer infoProvider);
final class Info extends Struct {
public final Signed32 data1;
public final Signed32 data2;
public R2VInfo(final Runtime runtime) {
super(runtime);
data1 = new Signed32();
data2 = new Signed32();
}
}
It works.
The above C++ method causes memory leak, so I would like to change it to return result by value:
extern "C" __declspec( dllexport )
Info __stdcall GetInfo(InfoProvider* infoProvider)
{
static_assert(std::is_pod<Info>::value, "Must be Plain Old Data in order to be safely copied between DLL boundaries");
Info info{};
Info.data1 = infoProvider->data1;
Info.data2 = infoProvider->data2;
return info;
}
I use the same Java JNR mapping:
Info GetInfo(Pointer infoProvider);
But it does not work - Access Violation. Native method is invoked, but with some dandling pointer value.
How to return by value in JNR?