Results 1 to 4 of 4

Thread: QThread and QTcpSocket

  1. #1
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QThread and QTcpSocket

    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)"

    Qt Code:
    1. void CServer::incomingConnection(int iSocketDescriptor)
    2. {
    3. CThread* pCThread = new CThread(this,iSocketDescriptor);
    4. bool bC = connect(pCThread, SIGNAL(finished()), pCThread, SLOT(deleteLater()));
    5. pCThread->start();
    6. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void CThread::run()
    2. {
    3. m_pqSocket = new QTcpSocket(this);
    4. if (!m_pqSocket->setSocketDescriptor(m_iSocketDescriptor))
    5. {
    6. emit error(m_pqSocket->error());
    7. return;
    8. }
    9. bool bC = connect(m_pqSocket,SIGNAL(readyRead()),this,SLOT(readData()));
    10. bool bC2 = connect(m_pqSocket,SIGNAL(disconnected()),this,SLOT(disconnected()));
    11. exec();
    12. }
    13.  
    14. CThread::~CThread()
    15. {
    16. m_pqSocket->disconnectFromHost();
    17. m_pqSocket->waitForDisconnected();
    18. delete (m_pqSocket); //Here the application crash
    19. }
    To copy to clipboard, switch view to plain text mode 

    I'm doing a similar code as the "threadedfortuneserver" example.

    Thanks,
    Òscar Llarch i Galán

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread and QTcpSocket

    The QThread object lives in the thread which created the object (so not the thread the QThread object represents), thus creating an object with the thread object as the parent within the run() method is not possible. Either skip the parent pointer of the socket (and delete the object before leaving run()) or move the QThread object to its own thread.

  3. #3
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QThread and QTcpSocket

    Hi,

    Thanks, It's working now.

    I thought that the objects created in the run method of the Thread will create the objects having the QThread as parent.

    Thanks,
    Òscar Llarch i Galán

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QThread and QTcpSocket

    A couple of additional notes:
    • Those connections established in CThread::run() should be enforced as Qt::DirectConnection. Otherwise the corresponding slots get executed in "wrong" thread context and using m_pqSocket in those slots will eventually lead to crashes.
    • The destructor of CThread shouldn't access m_pqSocket which lives in another thread.

    Easiest way to avoid aforementioned problems is to implement such functionality in QTcpSocket subclass instead of the QThread subclass.
    J-P Nurmi

Similar Threads

  1. Problem with QTcpSocket in QThread
    By Raistlin in forum Qt Programming
    Replies: 8
    Last Post: 6th October 2007, 12:23
  2. Strange QTcpSocket behavior (debugged with Ethereal)
    By mdecandia in forum Qt Programming
    Replies: 23
    Last Post: 28th May 2007, 20:44
  3. Problems with QThread and QTcpSocket
    By cookiem in forum Qt Programming
    Replies: 6
    Last Post: 2nd November 2006, 08:25
  4. QThread, QFile and QTcpSocket freezes GUI
    By naresh in forum Qt Programming
    Replies: 1
    Last Post: 23rd June 2006, 23:25
  5. QThread and QTcpSocket problem
    By probine in forum Qt Programming
    Replies: 1
    Last Post: 5th April 2006, 09:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.