Hi,
I have an application that has a QTcpServer. When a incoming connection comes, it creates a QThread passing the socketDescriptor to it. On "start", the QThread creates a QTcpSocket with this descriptor. The communication between client and server works, but when the client closes the communication and the server deletes the QThread(and the QThread deletes the QTcpSocket), the application crash:
"ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different threads. ..."
On console out, when the QThread sets the descriptor to the socket I recive this messages:
"QObject: Cannot create children for a parent that is in a different thread"
"(Parent is CThread(01901048), parent's thread is QThread(01738258), current thread is CThread(01901048)"
void CServer::incomingConnection(int iSocketDescriptor)
{
CThread* pCThread = new CThread(this,iSocketDescriptor);
bool bC = connect(pCThread, SIGNAL(finished()), pCThread, SLOT(deleteLater()));
pCThread->start();
}
void CServer::incomingConnection(int iSocketDescriptor)
{
CThread* pCThread = new CThread(this,iSocketDescriptor);
bool bC = connect(pCThread, SIGNAL(finished()), pCThread, SLOT(deleteLater()));
pCThread->start();
}
To copy to clipboard, switch view to plain text mode
void CThread::run()
{
if (!m_pqSocket->setSocketDescriptor(m_iSocketDescriptor))
{
emit error(m_pqSocket->error());
return;
}
bool bC = connect(m_pqSocket,SIGNAL(readyRead()),this,SLOT(readData()));
bool bC2 = connect(m_pqSocket,SIGNAL(disconnected()),this,SLOT(disconnected()));
exec();
}
CThread::~CThread()
{
m_pqSocket->disconnectFromHost();
m_pqSocket->waitForDisconnected();
delete (m_pqSocket); //Here the application crash
}
void CThread::run()
{
m_pqSocket = new QTcpSocket(this);
if (!m_pqSocket->setSocketDescriptor(m_iSocketDescriptor))
{
emit error(m_pqSocket->error());
return;
}
bool bC = connect(m_pqSocket,SIGNAL(readyRead()),this,SLOT(readData()));
bool bC2 = connect(m_pqSocket,SIGNAL(disconnected()),this,SLOT(disconnected()));
exec();
}
CThread::~CThread()
{
m_pqSocket->disconnectFromHost();
m_pqSocket->waitForDisconnected();
delete (m_pqSocket); //Here the application crash
}
To copy to clipboard, switch view to plain text mode
I'm doing a similar code as the "threadedfortuneserver" example.
Thanks,
Bookmarks