java - Safe to store pointer in JNI direct bytebuffer? -
i want create java object wrapper around native c++ object. putting pointer c++ object in direct bytebuffer 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 safe? meaning, java funny things pointer, maybe relocating or garbage-collecting it?
secondly, if not know size of world in advance (it forward-declared), ok give 0
buffer size? (provided, of course, not try read buffer)
the jvm free manage bytebuffer object contain same address , capacity. won't memory pointed bytebuffer unless call bytebuffer methods on , never move it.
but since aren't going call bytebuffer methods, bytebuffer , jlong equivalent. seems me using jlong instead make clearer, if have cast on in jni function. after all, java side, creating , storing handle, , jni side, treating handle pointer. cast makes clear.
Comments
Post a Comment