K
Kiran Kumar
I have a Native method "foo" which gets a "jstring" argument jStr:
JNIEXPORT jstring JNICALL foo (JNIEnv *jEnv, jclass jwclass, jlong
_this ,
jstring jStr) {
...
}
If I pass around the "jstring" argument jStr to other C++ methods, do
I have to
take care of any reference count issues for garbage collection for
jStr? Is it legal to do that as in the following?
JNIEXPORT jstring JNICALL foo (JNIEnv *jEnv, jclass jwclass, jlong
_this ,
jstring jStr) {
//Pass the "jstring" argument jStr to a C++ method DoSomething(jEnv,
jstring jStr) ...
//DoSomething() extracts the jStr chars using GetStringUTFChars(),
does something
//and releases jStr using ReleaseStringUTFChars()
DoSomething(jEnv, jStr);
}
I see thememory usage steadily go up when I run my java client program
that calls foo().
If I cut-n-paste the code in DoSomething() instead of calling it, I
see the memory usage stabilizes.
I suspect that passing a "jstring" jStr around (extra reference to
jStr?) to the DoSomething() routine is messing up the reference count
and the garbage collection does not occur for "jStr".
I noticed that only on Windows this is happening. I could not
reproduce this on Unix.
I have verified that the leak is happening in the call to
DoSomething(). As I mentioned if I replace the call to DoSomething()
by copying the code in DoSomething(), I see the memory usage
stabilize.
TIA for any helpful comments.
Regards
Kiran
JNIEXPORT jstring JNICALL foo (JNIEnv *jEnv, jclass jwclass, jlong
_this ,
jstring jStr) {
...
}
If I pass around the "jstring" argument jStr to other C++ methods, do
I have to
take care of any reference count issues for garbage collection for
jStr? Is it legal to do that as in the following?
JNIEXPORT jstring JNICALL foo (JNIEnv *jEnv, jclass jwclass, jlong
_this ,
jstring jStr) {
//Pass the "jstring" argument jStr to a C++ method DoSomething(jEnv,
jstring jStr) ...
//DoSomething() extracts the jStr chars using GetStringUTFChars(),
does something
//and releases jStr using ReleaseStringUTFChars()
DoSomething(jEnv, jStr);
}
I see thememory usage steadily go up when I run my java client program
that calls foo().
If I cut-n-paste the code in DoSomething() instead of calling it, I
see the memory usage stabilizes.
I suspect that passing a "jstring" jStr around (extra reference to
jStr?) to the DoSomething() routine is messing up the reference count
and the garbage collection does not occur for "jStr".
I noticed that only on Windows this is happening. I could not
reproduce this on Unix.
I have verified that the leak is happening in the call to
DoSomething(). As I mentioned if I replace the call to DoSomething()
by copying the code in DoSomething(), I see the memory usage
stabilize.
TIA for any helpful comments.
Regards
Kiran