Unsolved invokeMethod in other thread
-
Hi
I'm using this to init needed stuff in a worker thread - which works as supposed:
emit InitLoLaSocket(ip4addr, port, location, name, MAC, loglevel);
However, this is executed a lot later, whenever the event queue is coming to it.
I now want to have it execute immediately, and thought that this is the solution:QMetaObject::invokeMethod(LoLaWorker, "InitLoLaUDPsocket", Qt::DirectConnection, ip4addr, port, location, name, MAC, loglevel);
However, this returns expected primary-expression before ',' token and I simply can not see what I'm doing wrong.
Any idea?
Thanks, McL -
Hi,
in invokeMethod you need to use the Q_ARG functions to marshall the parameters e.g. like this to send a string:QString string; QMetaObject::invokeMethod(object,"slot",Q_ARG(QString,string));
-Michael.
-
Hi Michael,
Thanks. Corrected to this:
QMetaObject::invokeMethod(lolaworker, "InitLoLaUDPsocket", Qt::DirectConnection, Q_ARG(QString, ip4addr), Q_ARG(quint16, port), Q_ARG(QString, location), Q_ARG(QString, name), Q_ARG(QString, MAC), Q_ARG(QString, loglevel));
Any idea if nothing at all happens now?
-
why don't you just call
lolaworker->InitLoLaUDPsocket
instead of using invokeMethod? -
Because this builds correctly, but generates a signal 11 crash.
-
@McLion Are you sure this invokeMethod() call is executed?
-
@McLion said in invokeMethod in other thread:
Any idea if nothing at all happens now?
There can happen many things:
- you did not add Q_OBJECT to the class declaration
InitLoLaUDPsocket
is not in the slots section of the class- a typo in the slot name e.g. the original slot was
InitLoLaSocket
- you did not use the correct signature of the parameter e.g. forgot
*
or&
or used a wrong type LoLaWorker
has an invalid address- maybe more...
You can always debug into
invokeMethod
to get a hint about what it is that does not work.
-Michael. -
@jsulm
Yes.-
class LoLaWorker : public QObject
{
Q_OBJECT -
public slots:
void InitLoLaThread();
void InitLoLaUDPsocket(QString qsIP4addr, quint16 uiPort, QString qsLocation, QString qsName, QString qsMAC, QString qsLogLevel); -
InitLoLaSocket is the signal of the emitting class whereas InitLoLaUDPsocket is the slot of the target class running in another thread.
-
still thinking about the others ...
-
-
As said, I would debug into
invokeMethod
and make sure the functionInitLoLaUDPsocket
with the given list of parameters gets found in the list (or hash(?)) of slots and signals ofLoLaWorker
. -
@m.sue said in invokeMethod in other thread:
As said, I would debug into
invokeMethod
and make sure the functionInitLoLaUDPsocket
with the given list of parameters gets found in the list (or hash(?)) of slots and signals ofLoLaWorker
.I don't have a debugger on my eLinux Target.
-
invokemethod with direct connection is basically the same as a direct call to the method so I'd investigate why it generates a crash
-
The
invokeMethod
now calls it the same as the direct call - To be honest, I have no clue why the call works today - maybe the full rebuild did the trick ... and overnight ... ;-)
However, it still crashes.
Isn't calling this in a different thread the issue? Or I have some other issue - some code segments follow. I'm creating everything in the constructor of the mainwindow:// start a new thread and put the LoLaWorker to it bLoLaIsInitiated = false; QThread* lolathread = new QThread; LoLaWorker* lolaworker = new LoLaWorker(); lolaworker->moveToThread(lolathread); connect(lolathread, SIGNAL(started()), lolaworker, SLOT(InitLoLaThread())); lolathread->start(); ... some more connects here
I have
#include "LoLaWorkerThread.h"
andLoLaWorker *lolaworker;
public in header of mainwindow and I am callingQMetaObject::invokeMethod(lolaworker, "InitLoLaUDPsocket", Qt::DirectConnection, Q_ARG(QString, ip4addr), Q_ARG(quint16, port), Q_ARG(QString, location), Q_ARG(QString, name), Q_ARG(QString, MAC), Q_ARG(QString, loglevel));
When emitting the signal everything works - just not immediate, which is what I need.
I suspect I'm doing something wrong. Any ideas?
Thanks
McL -
@McLion said in invokeMethod in other thread:
QMetaObject::invokeMethod(lolaworker, "InitLoLaUDPsocket", Qt::DirectConnection, ...
This is a race condition if you're calling this from a thread different than
lolathread
.If you want something to be called first in a thread post the event to the object immediately after moving it to the thread (i.e. it becomes the first event) and don't use direct connection across threads if you don't provide manual access serialization.
// ... lolaworker->moveToThread(lolathread); // What's wrong with this? QMetaObject::invokeMethod(lolaworker, "InitLoLaUDPsocket", Qt::QueuedConnection, ... ); // And ... start the event processing lolathread->start();
-
@kshegunov
Sorry for the delay ... was called off to work on some other software.OK, I moved the moveToThread and the start from the constructor to the function where the initialization takes place (when, at runtime, I got the parameters needed), as suggested to try.
This gives me a crash at runtime (signal 11 - sigsegv - invalid memory reference).
-
that's segmentation fault... did you try debugging your code?
-
@VRonin
Working on Windows, compiling on/for Linux as well, running on eLinux device with no debugger. -
Can you compile for windows just for debug? or run a linux virtual machine to debug it?
I'm sorry to be this useless but that error is quite generic. Main things to investigate:
- Are you accessing an object through a pointer before creating it (calling new)?
- Are you accessing an object through a pointer after it was deleted?
- Are you trying to access a null pointer?
-
@VRonin
Thanks for trying to help!As far as I can tell everything is ok.
in constructor of qtgui mainwindow:
QThread* lolathread = new QThread; LoLaWorker* lolaworker = new LoLaWorker(); connect(lolathread, SIGNAL(started()), lolaworker, SLOT(InitLoLaThread())); connect(this, SIGNAL(TerminateLoLa()), lolaworker, SLOT(TerminateLoLaUDPsocket())); .. and some more conects
later, in function LoLaInit:
lolaworker->moveToThread(lolathread); QMetaObject::invokeMethod(lolaworker, "InitLoLaUDPsocket", Qt::QueuedConnection, Q_ARG(QString, ip4addr), Q_ARG(quint16, port), Q_ARG(QString, location), Q_ARG(QString, name), Q_ARG(QString, MAC), Q_ARG(QString, loglevel)); lolathread->start();
Anyhow, if moveToThread and start the thread are in the constructor and later emit a signal only, everything works, despite the fact that it is not immediate.
I start to believe that what I want is not possible.
I just want to have it to execute this function immediately (switching threads or whatever is needed) and following it up where it has left later (there is a queue of commands that now are worked off first, unfortunately). -
I just want to have it to execute this function immediately
Then just call the function from the main thread. invokeMethod with direct connection will execute it in the calling thread anyway so I still stuggle to understand why you are doing what you are doing
-
@VRonin said in invokeMethod in other thread:
Then just call the function from the main thread. invokeMethod with direct connection will execute it in the calling thread anyway so I still stuggle to understand why you are doing what you are doing
I tried to do this - to no avail. It may well be that it's me that is doing something wrong.
The debug console actually shows that it seems to really directly call the functions in the other thread. However, it also ends up in a strange behavior on the debug console - dbg strings already written out are written out again - and then I again get a seg fault crash.
There are 3 threads: gui-main, serial and lola. I want the following to happen
- serial gets parameters for lola-init and emit to gui-main (master cmd-interpreter)
- gui-main initializes lola (works with emit, but not immediately)
- lola tells gui-main success or not (works with emit, but not immediately)
When I replace the 2 emit with direct calls, I get the crash.
Maybe I'm doing something wrong .. or if what I want is not possible I'll think about a different approach/solution.
Thanks anyway.