lucene-pylucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andi Vajda <va...@apache.org>
Subject Re: JCC for Java -> C++ and initializeClass
Date Tue, 10 Mar 2015 18:54:25 GMT

On Tue, 10 Mar 2015, William Schilp wrote:

> i realize this question has been asked in the past (sept-2013) but the
> answer seems to be less than useful.
>
> i'm using JCC via C++ and having java crashing issues. the problem appears
> to be with the use of initializeClass(bool) and storing instances of JCC
> objects.

Before calling anything in Java, you must call initVM(...).
Then, before calling anything in Java in a thread other than main thread, 
you must also call attachCurrentThread(...)

> first off, what does initializeClass(bool) do and what does the boolean
> parameter mean/affect/do ... i see in other code that initializeClass tends
> to be called with false as the parameter but with no explanation.

initializeClass() is a static method defined on every java class wrapper you 
generated via JCC and must be called before you call anything on it (other 
than initializeClass, of course).
If you look at any of the generated .cpp files for your classes, you can 
find an initializeClass() method implementation that takes a bool.

If that bool is true, initializeClass() just returns the Java cls object (an 
instance of java.lang.Class), class$->this$, where class$ is the C++ wrapper 
object for your Java Class object instance and this$ is its corresponding 
Java instance object. If that bool is true and the class was not previously 
initialized, it returns NULL.
In other words, if you generated wrappers for org.apache.lucene.Document, 
class$ is the C++ org::apache::lucene::Document wrapper object and 
class$->this$ is the org.apache.lucene.Document.class object.

if that bool is false, and when class$ is still NULL, will cause 
initializeClass() to load and initialize the correspondong Java class via 
the Java VM (using findClass(...)) and the various method ids used to call 
methods on its instances via JNI will be initialized and cached.

> there is also no good explanation as to when initializeClass(bool) needs to
> be called and why. do i have to call it before accessing any method within
> the instance of the class? do i only call it once when constructing the
> instance of the class?

It must be called before the first Java access into any instance of this 
class.

> what happens if i call it twice on a class?

Nothing, it just returns the class$->this$ value obtained the first time.

> will this cause a crash?

No.

> can i just call initializeClass on all classes that i
> expect to instantiate at the beginning of the executable? i'm using 64bit
> java so memory usage is not a issue..

Yes, you can do this. Although it might be tricky to transitively close all 
these classes and it's not necessary, since anytime a C++ wrapper is
constructed, initializeClass() is called on its class, whether actually 
needed or not.

> is there any documentation on using JCC with C++, yes i have been to the
> pylucene webpage but there is only references to using JCC with python...

There is no specific C++ documentation besides what you already found.
But there is the generated source code. It's quite redable and indented.
All the answers I just gave you, I found them by looking at C++ 
wrapper source code generated by JCC.

If you have more questions, do not hesitate to ask.

Andi..

>
> bill schilp
>

Mime
View raw message