I want to create a java object wrapper around a native c++ object. I do this by putting the pointer to the c++ object in a direct ByteBuffer like so:
java side:
public class World {
private final ByteBuffer pointer;
public World() {
pointer = init();
}
private native ByteBuffer init();
public native void destroy();
}
native side:
extern "C" jobject Java_blabla_World_init(JNIEnv *e, jobject self) {
return env->NewDirectByteBuffer(new World, sizeof(World));
}
Is it safe? Meaning, will java do funny things to my pointer, like maybe relocating it or garbage-collecting it?
Secondly, if I do not know the size of World in advance (it was forward-declared), is it OK to just give 0
as the buffer size? (provided, of course, that I do not try to read from the buffer)