1

Sorry I’m not experienced enough to understand the error I’m getting and I have been trying now for weeks to understand what the problem is.

WHAT I NEED TO ACCOMPLISH:
Run multiple threads to control hardware and tests at the same time.

MY CODE:
I Followed the example provided here. This example is also available on many other sites so clearly it must work.

In my main code I have:

MotorClass *MotorObj = new MotorClass;
QThread *MotorThread = new QThread;

MotorObj->moveToThread(MotorThread);

connect(MotorThread, SIGNAL(started()), MotorObj, SLOT(RunMotor()));
connect(MotorObj, SIGNAL(finished()), MotorThread, SLOT(quit()));
connect(MotorObj, SIGNAL(finished()), MotorObj, SLOT(deleteLater()));
connect(MotorThread, SIGNAL(finished()), MotorThread, SLOT(deleteLater()));

MotorThread->start();

In my MotorClass.h I have:

class MotorClass : public QObject
{
    Q_OBJECT
public:
    explicit MotorClass(QObject *parent = 0);
    ~MotorClass();

public slots:
    void RunMotor();

signals:
    void finished();

};

In my MotorClass.cpp I have:

MotorClass::MotorClass(QObject *parent) : QObject(parent)
{
}

MotorClass::~MotorClass()
{
}

void MotorClass::RunMotor()
{
    qDebug("running");
    emit finished();
}


MY PROBLEM:
qDebug() does display the “running” message, but then it is followed by the following output and the program crashes.

QWinEventNotifier: event notifiers cannot be disabled from another thread
QWinEventNotifier: event notifiers cannot be disabled from another thread
QWinEventNotifier: event notifiers cannot be disabled from another thread
The program has unexpectedly finished.


WHAT I HAVE TRIED SO FAR:
If I comment out the connect code that is suppose to close the MotorThread and MotorObj, the errors disappear but obviously after running the program for long periods at a time causes it to crash. I understand this is because the MotorObj and MotorThread aren't being closed. I have tried other methods like inheriting the QThread class and then re-implementing the run() function but this does not cover my requirements and is also not the recommended way of using QThreads.

Any Ideas where I’m going wrong???

BaCaRoZzo
  • 7,502
  • 6
  • 51
  • 82
Achmed
  • 11
  • 1
  • 1
    Where do you start your thread (dont think the above line is the only one) and how often? – Robert Nov 13 '14 at 08:12
  • Nothing wrong in the code you posted. Do you have anything else? – thuga Nov 13 '14 at 09:09
  • Hi. I Have a MainWindow running as the main Thread. The MainWindow code contains the Main code you see above. From here I create The MotorThread and MotorObj. The code you see above from my MainWindow is called only once except for the MotorThread.start() which is called from a for loop in the MainWindow code. The for loop repeats about 5000 times over a period of a day. – Achmed Nov 13 '14 at 09:17
  • Could it happen that the `MotorThread.start()` is called successively without a delay in between? – Robert Nov 13 '14 at 09:22
  • This loop you have that calls `MotorThread->start();`. Does it check if `MotorThread` is still alive? I mean you did connect the `MotorClass::finished` signal to its `deleteLater` slot. – thuga Nov 13 '14 at 09:27
  • Recognized something odd: After first execution of your `RunMotor()` you emit a finished, which calls the `quit()`, which again deletes the MotorObj and MotorThread. So practically the slot might be called only once and fail MotorThread.start() should fail on next "execution"!? – Robert Nov 13 '14 at 09:28
  • What does the errors I see above mean? From what I hear other people saying it has something to do with me setting up the connections incorrectly? – Achmed Nov 13 '14 at 09:30
  • How do you display your qDebug messages? Try writing them to file or console, one must not write to UI from other treads than main. – Sebastian Lange Nov 13 '14 at 09:48
  • Robert I will just Go check my code again and get back to you. Thanks for your feedback. – Achmed Nov 13 '14 at 09:54
  • Sorry Robert. I Made a wrong statement above. All the code shown above is called from the for loop. In other words... For each iteration of the for loop the MotorThread and MotorObj is created new, then the MotorObj gets moved to the MotorThread, Then Connect the slots and signals, and then after this I call MotorThread.Start as shown above. But this Error occurs also is I only run the for loop once. – Achmed Nov 13 '14 at 10:04

2 Answers2

0
connect(MotorThread, SIGNAL(started()), MotorObj, SLOT(RunMotor()));
connect(MotorObj, SIGNAL(finished()), MotorThread, SLOT(quit()));
connect(MotorObj, SIGNAL(finished()), MotorObj, SLOT(deleteLater()));
connect(MotorThread, SIGNAL(finished()), MotorThread, SLOT(deleteLater()));

Looks like you are deleting the MotorThread after first execution has finished. So on next MotorThread->start() the program should crash, since it's only a dangling pointer. However, even if you remove the last connect, also the MotorObj does not exist anymore, so the slot might only be called once.

Why dont you call the deleteLater() right after the objects aren't needed anymore!?

Robert
  • 1,235
  • 7
  • 17
  • Sorry Robert. I Made a wrong statement above. All the code shown above is called from the for loop. In other words... For each iteration of the for loop the MotorThread and MotorObj is created new, then the MotorObj gets moved to the MotorThread, Then Connect the slots and signals, and then after this I call MotorThread.Start as shown above. But this Error occurs also is I only run the for loop once. – Achmed Nov 13 '14 at 10:21
  • 1
    I Think the problem is somewhere in my main code and not the code I posted above. I have now created a new program and copied the above code into it. Then it works. Meaning I'm doing something else wrong in my main program. Let me go check out my MainWindow code and I will let you know what I find. Thanks for your feedback. At least Im starting to zoom in on the problem. – Achmed Nov 13 '14 at 10:25
0

Ok. So after another week of sitting and trying to understand this weird problem, and I found a solution. Yet I don't understand why the problem occurred. I created new header and .cpp files for MotorClass and copied all the code from the original files to the new ones. Problem gone. Now everything works fine. Does not make sense at all though. But thanks for all your help guys.

Achmed
  • 11
  • 1