jni getFields 'repeated allocation of very large block'

  • Thread starter remi.chateauneu
  • Start date
R

remi.chateauneu

I try to get from a C++ program, with JNI, the list of fields of a
class, the goal being to get some reflection information on the C++
side, with this program:

int main( int argC, const char ** argV )
{
JavaVM *jvm;
JNIEnv *pEnv;
JavaVMInitArgs vm_args;
vm_args.version = JNI_VERSION_1_2;
vm_args.nOptions = 0;
vm_args.ignoreUnrecognized = true;

jint res = JNI_CreateJavaVM(&jvm, (void **)&pEnv, &vm_args);

jclass tstClass = pEnv->FindClass("MyClass");

jmethodID initId = pEnv->GetMethodID(tstClass,"<init>","()V");

jobject tstObjct = pEnv->NewObject(tstClass, initId );

jclass jlClss = pEnv->FindClass("java/lang/Class");

jmethodID getFlds = pEnv->GetMethodID(jlClss, "getFields", "()
[Ljava/lang/reflect/Field;");

/// 'Repeated allocation' starts from here...
jobjectArray fields = (jobjectArray)pEnv-
CallObjectMethod(tstObjct, getFlds);
...
jvm->DestroyJavaVM();
};

.... and this class :
class MyClass {
public String Nam = null;
public int Num = 0;

public MyClass() {
this("Alex", 123);
}
}

.... and I get this output after GetMethodId("getFields"):

GC Warning: Repeated allocation of very large block (appr. size
512000):
May lead to memory leak and poor performance.
GC Warning: Repeated allocation of very large block (appr. size
512000):
May lead to memory leak and poor performance.
.... etc ... (Endless)

I checked all returned values of JNI functions, they are fine.
gcj 4.2.2
gcc 4.2.2


Any idea, please ? Thanks.
 
G

Gordon Beaton

/// 'Repeated allocation' starts from here...
jobjectArray fields = (jobjectArray)pEnv->CallObjectMethod(tstObjct, getFlds);

Your tstObjct is a MyClass, not a java/lang/Class, so it doesn't have
the getFields method you're attempting to invoke here. Specify
tstClass instead.

/gordon

--
 
R

remi.chateauneu

Your tstObjct is a MyClass, not a java/lang/Class, so it doesn't have
the getFields method you're attempting to invoke here. Specify
tstClass instead.

/gordon

--

Many thanks, well done - Wonder why it is not detected at runtime...
 
G

Gordon Beaton

Many thanks, well done - Wonder why it is not detected at runtime...

Like most C libraries, JNI functions do not check for programming
errors!

Also, both tstObjct and tstClass are valid first arguments to
CallObjectMethod(), since they're both object references.

/gordon

--
 
L

Lew

Gordon said:
Like most C libraries, JNI functions do not check for programming
errors!

And because Java does check for many common programming errors, some
programmers whine that it's too "restrictive".

So we complain when Java forces us to be careful, and more when it doesn't.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top