Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2008

Tip: Looking for answers? Try searching our database.

jni getFields 'repeated allocation of very large block'

Thread view: 
remi.chateauneu@gmail.com - 09 Jan 2008 01:46 GMT
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.
Gordon Beaton - 09 Jan 2008 08:14 GMT
> /// '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

--
remi.chateauneu@gmail.com - 09 Jan 2008 08:46 GMT
> > /// 'Repeated allocation' starts from here...
> >     jobjectArray fields = (jobjectArray)pEnv->CallObjectMethod(tstObjct, getFlds);
[quoted text clipped - 6 lines]
>
> --

Many thanks, well done - Wonder why it is not detected at runtime...
Gordon Beaton - 09 Jan 2008 09:17 GMT
> 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

--
Lew - 09 Jan 2008 14:07 GMT
>> Many thanks, well done - Wonder why it is not detected at runtime...
>
> 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.

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.