[Interest] Trouble with Android JNI and QEventLoop

Marc Van Daele marc.van.daele90 at gmail.com
Mon Jul 13 11:35:50 CEST 2020


Can you also print the thread-pointer/id next to the name?
Maybe (just guessing) one QtMainLoopThread is from the main app and the
other one is from the service?
You could add a QMutexLocker (https://doc.qt.io/qt-5/qmutexlocker.html) to
ensure that the calls are executed sequentially.

Kind Regards,

Marc

On Mon, 13 Jul 2020 at 09:25, Fabrice Mousset | GEOCEPT GmbH <
fabrice.mousset at geocept.com> wrote:

> 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
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> https://lists.qt-project.org/listinfo/interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20200713/a3f2dd42/attachment.html>


More information about the Interest mailing list