[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

NewStringUTF gives memory leak?

Posted on 2000-02-16
8
Medium Priority
?
1,416 Views
Last Modified: 2012-06-21
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?
0
Comment
Question by:zingo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 5

Expert Comment

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

>>>(they crash the app )

What platform Windows /Unixes?
0
 

Author Comment

by:zingo
ID: 2526871
The platform is Windows and I am using Visual J++.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:mbormann
ID: 2526910
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.
0
 

Author Comment

by:zingo
ID: 2527343
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.

0
 

Accepted Solution

by:
ChristerH earned 400 total points
ID: 2527350
Use this code to delete the reference, then the JVM's garbage collector will deallocate the memory.

tmp_env->DeleteLocalRef(jstr);
 
 
0
 

Author Comment

by:zingo
ID: 2527362
Thanks ChristerH, you saved my day!
0
 
LVL 5

Expert Comment

by:mbormann
ID: 2530054
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.
:~)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question