[Qt-jambi-interest] Jambi and JNI Integration

Eskil Abrahamsen Blomfeldt eblomfel at trolltech.com
Tue Jul 8 12:18:12 CEST 2008


Adam Batkin wrote:
> Anyway, I've now set out to write my own JNI code to do what I want, instead of using the 
> Jambi Generator. Everything was working nicely until trying to transfer a QImage from C++ 
> to a Java/Jambi QImage. Back in February, Gunnar suggested stealing some logic from the 
> generated Jambi code, in particular QPixmap::toImage():
>      jobject jimg =
>          qtjambi_from_object(env,
>                              &image,
>                              "QImage",
>                              "com/trolltech/qt/gui/",
>                              true);
>
> This works -- almost. I get back a valid Java QImage object (System.err.println(image) 
> prints something like com.trolltech.qt.gui.QImage at d0a5d9) but when I try to do anything 
> with it (like call image.width()), the JVM crashes.
>   

Hi, Adam.

Has Qt Jambi been properly initialized at this point? I.e. you need to 
initialize each of the libraries you use before they can be used, 
otherwise Qt Jambi won't know e.g. that it has to copy the QImage into a 
new object before linking it into a Java object, and you will get a Java 
object with a pointer into the stack. The initialization of Qt Jambi 
libraries is done the first time a class from the library is loaded by 
the virtual machine. In your case, I think you are calling the 
conversion code before loading any Jambi GUI classes. The conversion 
code itself will resolve and load the QImage class, thus initializing 
the GUI library, but unfortunately this is too late for the first 
conversion, and the first Java object you retrieve will contain a 
pointer to the object you allocated on stack. This is why you can fix 
the problem by manually loading the class using the resolveClass() call, 
or by calling another method which causes the library to be initialized.

We can easily fix this particular problem by moving some code around, 
and I'll make a suggestion to do so. However, I'm not convinced that 
we'll be able to fix all related issues, so I recommend you initialize 
the library prior to using Qt Jambi. For instance, we don't support 
having Qt Jambi application that have not called 
QApplication.initialize() or QCoreApplication.initialize() (the former 
in your case.) If you call QApplication.initialize() in the beginning of 
your application, this should cause the GUI library to be initialized.

-- Eskil





More information about the Qt-jambi-interest mailing list