QThread errors
-
Hello... I am trying to receive and send a packet to game server... However, app does not send it because "socket notifiers cannot be enabled from another thread"... I am actually getting two errors...
QObject: Cannot create children for a parent that is in a different thread.
(Parent is NetworkThread(0x12cd950), parent's thread is QThread(0x121fb58), current thread is NetworkThread(0x12cd950)QSocketNotifier: socket notifiers cannot be enabled from another thread... This error appears when code below is executed... App does not crash or anything, just here everything ends, because it does not send packet to server...
@sSocket->write((char*)outdata, outsize);@
Here is my project "GitHub":https://github.com/SeaWave7/iTool
Code above is located in packetprocessor.cpp - line 78
-
Figured out what part causes first error (QObject) but don't know how to solve it... I would really appreaciate if someone could tell me how to fix these two problems... I can't move on without solving this...
@void NetworkThread::run()
{
// Create a new socket
sSocket = new QTcpSocket(this); // QObject error appears here !!!// Create a new packet processor
pProcessor = new PacketProcessor(sSocket);// Set the receive index
recvIndex = 0;// Connect the read event to a slot //Qt::DirectConnection
connect(sSocket, SIGNAL(readyRead()), this, SLOT(readData()));// Disconnected
connect(sSocket, SIGNAL(disconnected()), this, SLOT(socketDisconnected()));// Random seed
srand(getTickCount());// Random server
switch(rand() % 4)
{
case 0:
sSocket->connectToHost("121.128.133.26", 15779);
break;
case 1:
sSocket->connectToHost("121.128.133.27", 15779);
break;
case 2:
sSocket->connectToHost("121.128.133.28", 15779);
break;
case 3:
sSocket->connectToHost("121.128.133.29", 15779);
break;
case 4:
sSocket->connectToHost("121.128.133.30", 15779);
break;
default:
sSocket->connectToHost("121.128.133.26", 15779);
}// Process thread events
exec();
}@ -
Wah... First error message has gone when I changed
from this
@ sSocket = new QTcpSocket(); @
to this
@ sSocket = new QTcpSocket();@Second error is still there...
@qDebug() << "Writing socket";
sSocket->write((char*)outdata, outsize);
qDebug() << sSocket->error();@Output:
Writing socket
QAbstractSocket::UnknownSocketError
DisconnectedEDIT: For some reason socket gives "Unknown error" right after it has been created...
@ // Create a new socket
sSocket = new QTcpSocket(this);
qDebug() << sSocket->error(); // QAbstractSocket::UnknownError
@ -
Hi,
[quote]
@
// Connect the read event to a slot //Qt::DirectConnection
connect(sSocket, SIGNAL(readyRead()), this, SLOT(readData()));
@
[/quote]1) You subclassed QThread and put a new slot in it. This is wrong.From the "QThread documentation":http://qt-project.org/doc/qt-5/qthread.html:
"It is important to remember that a QThread instance lives in the old thread that instantiated it, not in the new thread that calls run(). This means that all of QThread's queued slots will execute in the old thread. Thus, a developer who wishes to invoke slots in the new thread must use the worker-object approach; new slots should not be implemented directly into a subclassed QThread.
When subclassing QThread, keep in mind that the constructor executes in the old thread while run() executes in the new thread."
The QThread documentation gives you an example of how to use a worker object.
[quote]
@void NetworkThread::run()
{
// Create a new socket
sSocket = new QTcpSocket(this); // QObject error appears here !!!
@
[/quote]2) See the official documentation about Thread Affinity: http://qt-project.org/doc/qt-5/QObject.html#thread-affinityPay attention to what it says about parents. It will show you why you got this error.
[quote author="Anonymous" date="1394894153"]EDIT: For some reason socket gives "Unknown error" right after it has been created...
@ // Create a new socket
sSocket = new QTcpSocket(this);
qDebug() << sSocket->error(); // QAbstractSocket::UnknownError
@[/quote]3) There is nothing wrong here. It says "UnknownError" because there is no error.