1

I am new to multithreading. It seems many people have a similar problem. I searched a lot for an answer. However, I cannot figure out why I get the following error in my case:

ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread 26ff4538. Receiver '' (of type 'RuleSetVerifier') was created in thread 90b6c0", file kernel\qcoreapplication.cpp, line 553

From my main thread I start a QRunnable thread:

ConditionAnalysisManager* analysisManager = new ConditionAnalysisManager(param1, param2);
    connect(analysisManager, SIGNAL(conditionAnalysisResultReady(const QString&, int, bool, QMap<QString, bool>, const QString&)), this, SLOT(evaluateAnalysisResult(const QString&, int, bool, QMap<QString, bool>, const QString&))); 
    QThreadPool::globalInstance()->start(analysisManager);

The result calculated in the thread are sent back to the main thread via a signal/slot connection. In the ConditionAnaylsisManager I do the following:

void ConditionAnalysisManager::run()
{
     this->analyzeConditions(param1, param2);
}

void ConditionAnalysisManager::analyzeConditions(int hostId, const QString& param2)
{
 //calculations
    emit conditionAnalysisResultReady(uid, hostId, finalResult,  ruleSetResults, finalDataString);
}

The Alert happens when the signal is emitted. I tried to implement it as described in http://wiki.qt.io/Threads_Events_QObjects#Signals_and_slots_across_threads

As far as I understand it is possible to call a slot in the main thread from a thread. I tried with Qt::QueuedConnection and with default. I always get the Alert. Why?? What is wrong in my case?

In the constructor of the ConditionAnalysisManager class that inherits QRunnable I create an object of a helper class which does not inherit from QRunnable.

this->m_ruleSetVerifier = new RuleSetVerifier(param1, param2);

I mention this because the Alert says " Current thread 26ff4538. Receiver '' (of type 'RuleSetVerifier')". I cannot understand why the thread is of type RuleSetVerifier and not of type ConditionAnaylsisManager since the signal is emitted from the ConditionAnalysisManager. What is wrong??

I am looking forward to your help. Thank you!

Natalie
  • 445
  • 2
  • 5
  • 18
  • And how do you use this `RuleSetVerifier` object? – thuga Apr 15 '16 at 10:17
  • I call methods in this class and return the results. There are no signal/slots in the RuleSetVerifier. – Natalie Apr 15 '16 at 11:02
  • 1
    There is too much code missing to be able to find the source of the problem. You seem to be trying to do something with the `RuleSetVerifier` object from the wrong thread. You should know that it lives in the main thread, while everything you do in `ConditionAnalysisManager::run` is executed on the new thread. – thuga Apr 15 '16 at 11:05
  • Thanks! Why is the RuleSetVerifier object in the main thread when I create it in the ConditionAnalysisManager thread? It should be in the ConditionAnalysisManager thread. How can I get it there? Should I do moveToThread? – Natalie Apr 15 '16 at 11:47
  • 1
    Because you create it in the constructor. The only part which runs in the new thread is what happens in the `run` method. So you have create it in the `run` method, and make sure you only use it the new thread. Or you can use the [worker object](http://doc.qt.io/qt-5/qthread.html#details) design. – thuga Apr 15 '16 at 11:58
  • Thanks a lot! I thought that every object I make in the new thread belongs to the new thread. I will try the worker object design, since the RuleSetVerifier has also a helper class and I want all this to be running in the new thread. I hope that this is possible. – Natalie Apr 15 '16 at 12:04
  • In Qt, when you call [`moveToThread`](http://doc.qt.io/qt-5/qobject.html#moveToThread), it moves all children to a new thread as well. Just make sure you set the parent to your child objects. – thuga Apr 15 '16 at 12:28
  • This question has way too little detail to figure out. Minimize your code. Throw out *everything* that's unnecessary to reproduce, then shove it all of it into a single `main.cpp` (like e.g. [here](http://stackoverflow.com/a/36577062/1329652)). Then update the question to include the minimal `main.cpp`. You shouldn't need more than a 100 lines to show the problem. All I see at the moment is that it works for me if I fill in the blanks that you didn't provide. As it stands, the question is off-topic. If you ask about code, you *must provide it*. – Kuba hasn't forgotten Monica Apr 15 '16 at 13:34
  • I think my code is minimized. If you read the comments you will see that thuga (thanks a lot!) already pointed me to what I understood wrong. I am trying to fix it. This is a bit difficult since the project is pretty complex. – Natalie Apr 15 '16 at 13:46

0 Answers0