Solved

NewStringUTF gives memory leak?

Posted on 2000-02-16
8
1,290 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
  • 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
 
LVL 5

Expert Comment

by:mbormann
ID: 2526841
0
 

Author Comment

by:zingo
ID: 2526871
The platform is Windows and I am using Visual J++.
0
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 100 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now