memory leaks jboss-java with jni interface

J

johan

Hello,

I have a very strange problem.
I'm using jboss3.2.3 with java 1.4.1_02 on linux redhat 7.3

The webserverapplication uses a jni connection to a own written linux
library.
Everything works very fine except a memory leak.

I have checked my java application on leaks with jprofiler and
everything seams cleaned-up nice.
I also have checked my c-application on mem-leaks and also there we
don't have any leak.

So it must be somwhere in jni interface? Anyone a id how to find this
leak?
I have maked 2 big functions java_2_cpp and cpp_2_java thats transfers
the data between the lib an the java webserver

thx

Johan

//==================== part of code ================//
java_2_cpp(JNIEnv *env,jobject obj)

case dm_astring:{
fid=0x00;
cls = env->GetObjectClass(current_obj);
fid=env->GetFieldID(cls,members.element_name,"Lcom/arrowup/gva/data/AString;");
if(fid==NULL){
break;
}
m_string=env->GetObjectField(current_obj,fid);
fid=0x00;
cls = env->GetObjectClass(m_string);
fid=env->GetFieldID(cls,"value","Ljava/lang/String;");
if(fid==0x00){
break;
}
jstring jstr = (jstring)env->GetObjectField(m_string,fid);
if(jstr!=0x00){
p_str= env->GetStringUTFChars(jstr,0);
*(AString *)members.ptr = p_str;


env->ReleaseStringUTFChars((jstring)jstr,p_str);
}
break;
}

//=====================================================//
cpp_2_java(JNIEnv *env,jobject obj)
case dm_astring:{
fid=0x00;
cls = env->GetObjectClass(current_obj);
fid=env->GetFieldID(cls,members.element_name,"Lcom/arrowup/gva/data/AString;");
if(fid==0x00){
break;
}
m_string=env->GetObjectField(current_obj,fid);
fid=0x00;
cls = env->GetObjectClass(m_string);
fid=env->GetFieldID(cls,"value","Ljava/lang/String;");
if(fid==0x00){
printf("Nothing found 4444 !!!\n");
break;
}
char *p_str=NULL;
p_str=((AString *)members.ptr)->str();
if(p_str!=NULL){
if(strlen(p_str)!=0x00){
jstring jstr = env->NewStringUTF(p_str);
env->SetObjectField(m_string,fid,(jobject)jstr);
}
}
break;
}
 
G

Gordon Beaton

The webserverapplication uses a jni connection to a own written
linux library. Everything works very fine except a memory leak.

I have checked my java application on leaks with jprofiler and
everything seams cleaned-up nice. I also have checked my
c-application on mem-leaks and also there we don't have any leak.

You seem to contradict yourself. You say that there is a leak, but
your tools say that there isn't. How are you making this observation?
So it must be somwhere in jni interface? Anyone a id how to find
this leak? I have maked 2 big functions java_2_cpp and cpp_2_java
thats transfers the data between the lib an the java webserver

I can't see what is causing any leak, but the following is extremely
suspect:
p_str= env->GetStringUTFChars(jstr,0);
*(AString *)members.ptr = p_str;
env->ReleaseStringUTFChars((jstring)jstr,p_str);


After calling ReleaseStringUTFChars() the string, you cannot continue
to use p_str that you stored in the members array, because it points
to memory that has now been freed. You need to make a *copy* of the
string contents if you want to use it after releasing it.

What is AString? GetStringUTFChars() doesn't return any such type. Is
this cast valid?

This is part of the corresponding code in the other function where you
use the stored pointer:
p_str=((AString *)members.ptr)->str();


What is str(), and what assumptions does it make about the contents of
the pointed-to array? Does it *allocate* anything?

/gordon
 
J

johan

You seem to contradict yourself. You say that there is a leak, but
your tools say that there isn't. How are you making this observation?

If i see with "top" in linux we see that the memory that java takes
evrytime goes up.
I can't see what is causing any leak, but the following is extremely
suspect:
p_str= env->GetStringUTFChars(jstr,0);
*(AString *)members.ptr = p_str;
env->ReleaseStringUTFChars((jstring)jstr,p_str);


After calling ReleaseStringUTFChars() the string, you cannot continue
to use p_str that you stored in the members array, because it points
to memory that has now been freed. You need to make a *copy* of the
string contents if you want to use it after releasing it.

What is AString? GetStringUTFChars() doesn't return any such type. Is
this cast valid?

AString is our own written classe for handelings strings, and yes teh
casti,ng is correct
This is part of the corresponding code in the other function where you
use the stored pointer:
p_str=((AString *)members.ptr)->str();


What is str(), and what assumptions does it make about the contents of
the pointed-to array? Does it *allocate* anything?


str() give the char* pointer back of the string
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top