If I have a class FunctionsClass that inherits QObject and has a QTcpServer and QTcpClient in it as member variables. In my main UI class I set up the FunctionsClass member variable on a Qthread called FunctionClassThread by doing:
FunctionsClass *classObj = new FunctionsClass // (classObj is a class member)
classObj->moveToThread( &FunctionClassThread );
FunctionClassThread.start();
// connected some signal/slots..
// do some stuff...
Then I deconstruct the object containing my instance of FunctionsClass by doing:
FunctionClassThread.quit();
FunctionClassThread.wait();
delete classObj; // problem line...........
This error only happens when classObj's QTcpServer is currently listening or itsQTcpSocket is attempting to connect to a server and I delete classObj. The error is: ASSERT failure in QCoreApplication::sendEvent: “Cannot send events to objects owned by a different thread.
Any ideas?
After moving
classObjto another thread, you can no longer delete it from the thread you created it in. You either have to delete it within yourFunctionsClassThreador move it back to the main thread via code which is processed within theFunctionsClassThread.Read the Qt docs on moveToThread():
So just before your FunctionsClassThread is about to stop, it has to call
Of course your FunctionsClassThread needs to be aware of the object, either directly, or by using signals & slots