Returning new objects from C++ to Java
Hi,
First off thanks a lot for this project, it makes working with the JNI much nicer!
Now to my issue: I have a native method that is expected to return a newly generated object (an ArrayList in my case, but the issue is the same for all object types). I successfully created it using your Object::newInstance() helper method and could also modify it as I wanted, but then when I want to return it to Java (the native method returns a jobject, so I used Object::getHandle() as the return value), the calling Java code receives a null pointer. After some digging I found out that this is because just before returning, the Object that I created inside my native method is destroyed, leading to the destructor being called and the JVM reference being deleted. My current workaround is to add a new boolean flag to Object that is set to true if the object should be "persistent" in the JVM, making the destructor return without further actions. But I'm wondering whether there is a better way to do it that I simply didn't discover yet.