Link to home
Start Free TrialLog in
Avatar of zingo
zingo

asked on

NewStringUTF gives memory leak?

I have a thread running in native code which needs to call a static Java method with a jstring parameter.

Code:

jstring            jstr;
char            szTxt[MAX_DEBUG_OUT_LEN];
jclass            cls;
jmethodID      mid;

// Init code cut away here...

jstr = tmp_env->NewStringUTF(szTxt);
tmp_env->CallStaticVoidMethod( cls, mid, jstr );


The problem I got is that the NewStringUTF seems to allocate memory, and I don't know how to deallocate it. The JVM doesn't seem to take care of it, "delete" does not work, and ReleaseStringUTFChars does not work either (they crash the app ).

How do I get rid of the memory?
Avatar of mbormann
mbormann

clicl around this thread
http://sourceware.cygnus.com/ml/cygwin/1998-10/msg00615.html

>>>(they crash the app )

What platform Windows /Unixes?
Avatar of zingo

ASKER

The platform is Windows and I am using Visual J++.
I think taht from that site the code to free jstring is

const char *str = (*env)->GetStringUTFChars(env, Your_jstring, 0);
(*env)->ReleaseStringUTFChars(env, Your_jstring, str);

please let me know if this helps you out.
Avatar of zingo

ASKER

nope, it still leaks like a bottom-less bucket...

According to the documentation and examples from Sun, ReleaseStringUTFChars is used together with GetStringUTFChars. I've never seen it used with NewStringUTF.

ASKER CERTIFIED SOLUTION
Avatar of ChristerH
ChristerH

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zingo

ASKER

Thanks ChristerH, you saved my day!
Oops ,and a thanks from me too, what we did was we had no need to track these as our .exe used to call Java object and go off.

I will bookmark this question.As they say u never stop learning.
:~)