[Interest] Trouble with Android JNI and QEventLoop

Fabrice Mousset | GEOCEPT GmbH fabrice.mousset at geocept.com
Mon Jul 13 09:23:42 CEST 2020


Hi all,

First, I tried to send this mail to Android mailing list, but got an error message as reply, so I try here. Sorry if I am wrong

I have a random issue with one of my Android service I've build with Qt 5.12.9.
I have centralized JNI interface in one C++ class, which is a singleton.
My service is single threaded, so there should not be a threading issue... I guess!

So here my issue: I have a function which will populate a list. This function could be called at service begin or while receiving a specific Broadcast message.
Sometimes, it happens that this function is called twice before first call is finished.

I have at some traces to follow what's happening, something like:
void MyService::updateList(int calledFrom) {
    qDebug() << "Update List start #"<< calledFrom << "@" << QThread::currentThread()->objectName();
    ...
    qDebug() << "Update List end #"<< calledFrom << "@" << QThread::currentThread()->objectName();}
}

And, on LogCat I can see:
Update List start # 0 @ "QtMainLoopThread"
Update List start # 1 @ "QtMainLoopThread"
Update List end # 1 @ "QtMainLoopThread"
Update List end # 0 @ "QtMainLoopThread"


Here is the way I handle JNI calls:
void JniHandler:: stateChanged(JNIEnv *, jobject, jint newState) {
    auto* pInstance = JniHandler::instance();
    if(pInstance)
    {
        QMetaObject::invokeMethod(pInstance, [pInstance , newState] {
            ...
            });
     }
}

I tried to change QMetaObject::invokeMethod call to add ' Qt::QueuedConnection', but didn't change anything.
I also change to use QTimer::singleShot():
    if(pInstance)
    {
        QEventLoop myLoop;
        Q_UNUSED(myLoop)
        QTimer::singleShot(0, pInstance, [pInstance , newState] {
            ...
            });
     }

But still have the same issue.

How is this possible?
What I am doing wrong?

Best regards

Fabrice Mousset



More information about the Interest mailing list