user1086579
user1086579

Reputation: 81

Will function result passed as parameter be released in JNI?

So my question is concerned with memory leak in JNI.

if I have:

jclass cls = env->FindClass("java/lang/String");
jobjectArray aRow = env->NewObjectArray(col, cls, NULL);
env->DeleteLocalRef(cls);

I know cls memory will be release. However if I use:

jobjectArray aRow = env->NewObjectArray(col, env->FindClass("java/lang/String"), NULL);

Will the jclass passed as parameter be released?

Upvotes: 1

Views: 90

Answers (1)

Pavel Zdenek
Pavel Zdenek

Reputation: 7293

Yes, but not immediately, only after your current JNI method returns to the JVM caller. Then GC will take care about it. This should not be a problem as long as you are creating a modest amount of references. JVM will tell you when your amount is not modest anymore.

However, for complete correctness, you should check the return value of FindClass before you use it as a parameter elsewhere. It can still return NULL in case JVM throws an exception.

Upvotes: 1

Related Questions