newbie question on JNI passing JString to other C++ routines ...

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
 
G

Gordon Beaton

I have a Native method "foo" which gets a "jstring" argument 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".

Java's garbage collection is based on reachability, not reference
counting. Reachability is affected by things like passing the String
across the Java/C boundary (as an argument to a native method or a
return a value from a native method) or calling certain JNI API
functions.

In other words you can pass around the jstring argument among your C
functions as much as you like and the JVM has no knowledge of it. It
has no way of seeing what your native code is up to until you invoke a
function through the JNIEnv*.

I suspect your observation is either flawed or due to something
completely different. Without seeing any real code it's hard to be
more specific.

/gordon
 
K

Kiran Kumar

Gordon,

My observation is incorrect. I found the problem to be something else
and now it's fixed.

Thanks for your reply ...
Kiran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top