How to free C++ instance in JNI?

63 Views Asked by At

I want to know how to free the instance of c++ class. I tried like this but error occurred.

Live *pLive = Live::getInstance();
// do something with pLive
env->DeleteLocalRef(reinterpret_cast<jobject>(pLive));

I need your help. Thank you.

1

There are 1 best solutions below

1
On

Since you're not calling anything on env to create the object, I assume Live::getInstance returns a C++ object and not a JNI object? In that case, use delete. You only use DeleteLocalRef on a JNI object. (A JNI object is an object that actually exists inside the JVM and is directly accessible in Java). Trying to DeleteLocalRef on a C++ object will always crash, as its not in the JVM's shared memory