QPainter JNI call crashes the application



  • Hi,

    What I am trying to do -
    Make a JNI call to a shared library written in Qt. On the C++ side I am using QPainter and QSvgRender to manipulate an svg image and return a simple QImage.

    @ QImage im;
    QPainter painter;
    if("image/svg+xml" == mimeType) {
    QSvgRenderer svgrenderer(ar);
    im = QImage(static_cast<int> (svgrenderer.defaultSize().width()),
    static_cast<int> (svgrenderer.defaultSize().height()),
    QImage::Format_ARGB32);
    im = im.scaled(QSize(50,50), Qt::KeepAspectRatio);
    im.fill(0);
    painter.begin(&im);
    svgrenderer.render(&painter); //Crashes here
    }@

    What is the issue -

    The code crashes while calling render as shown. And it crashes only for certain SVG images.

    If I run the code within a stand alone Qt Application, it works fine.
    I hadn't created a QApplication instance within my shared lib as against a stand alone Qt App.
    So I decided to create one this way-

    From Java Main Thread - Spawn another thread which makes a JNI call to create a QApplication. Runs exec().

    From Java Main Thread continue to make other JNI calls after the QApplication is initialized.

    This still doesn't work. Any advise on what I may be doing wrong and what is the right way to do what I am trying to do?
    I have searched the forum and the web for solutions but nothing has helped. The only thread that came close to what I need is - "http://developer.qt.nokia.com/forums/viewthread/2283":http://developer.qt.nokia.com/forums/viewthread/2283.

    The whole thing runs inside a java based web container.



  • Apart from some polishing (you're not calling painter.end(), those static_cast are not needed at all, line 8 is absolutely nonsense (you're scaling an unitializated image then you fill it, so why don't you create an image of the right size from the beginning?)), the snippet seems ok, so the problem may be in any other part of the program. Can you write a small, self-contained, compilable example that crashes? (Eventually, by providing a SVG that makes it crash...)



  • [quote author="peppe" date="1292837685"]Apart from some polishing (you're not calling painter.end(), those static_cast are not needed at all, line 8 is absolutely nonsense (you're scaling an unitializated image then you fill it, so why don't you create an image of the right size from the beginning?)), the snippet seems ok, so the problem may be in any other part of the program. Can you write a small, self-contained, compilable example that crashes? (Eventually, by providing a SVG that makes it crash...)[/quote]

    Thanks peppe for your thoughts. I understand what you've explained.

    1. I am calling painter.end() which is not shown in the code 'snippet' I've posted.
    2. Regarding line 8, I understand what you mean. But it works for me for images that don't crash the app. I need the svg to be scaled to 50x50 while keeping the aspect ratio. I do get the images scaled to 50x50. BUt like you said I could probably create an image of the right size from the beginning.
    3. I will try to write a compilable and attach it for you and others to see. Like I said this is happening as a part of a JNI call.

    I was hoping someone would give me some advice on how to use gui realted classes inside a shared library in a proper way and to be able to make calls from java via JNI.



  • Kind of solved the crashing issue. Seemed to be a bug in Java 1.5 that I was using http://bugs.sun.com/view_bug.do?bug_id=5102720

    I have fixed it for now by changing qt to make up for the stack realignment issue by adding these flags for qmake- QMAKE_CFLAGS += "-mstackrealign" QMAKE_CXXFLAGS += "-mstackrealign"

    The problem was mainly on windows



  • I used it , but when I compile it, there is error:

    cc1plus: error: unrecognized command line option ?-mstackrealign?

    I was use qpainter in jni on linux.

    Please help me !!

    [quote author="karthiksrini" date="1294042458"]Kind of solved the crashing issue. Seemed to be a bug in Java 1.5 that I was using http://bugs.sun.com/view_bug.do?bug_id=5102720

    I have fixed it for now by changing qt to make up for the stack realignment issue by adding these flags for qmake- QMAKE_CFLAGS += "-mstackrealign" QMAKE_CXXFLAGS += "-mstackrealign"

    The problem was mainly on windows[/quote]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.