?
Solved

NewStringUTF gives memory leak?

Posted on 2000-02-16
8
Medium Priority
?
1,382 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
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

Industry Leaders: 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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month13 days, 14 hours left to enroll

800 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