Solved

NewStringUTF gives memory leak?

Posted on 2000-02-16
8
1,317 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
reverse digits of a number using for loop 5 52
check java version using powershell 13 168
String array comparison 4 38
Java basic valueOf question 1 29
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
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 …

807 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