Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] SIGSEGV when TCP connection breaks during waitForBytesWritten or waitForReadyRead

[solved] SIGSEGV when TCP connection breaks during waitForBytesWritten or waitForReadyRead

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 5.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nirslsk
    wrote on last edited by
    #1

    Hi there,

    I'm writing a P2P client using Qt (thanks by the way, Qt has been amazing so far!), and I'm trying to re-implement some existing file transfer code in a separate thread instead of just using QTcpSocket events in the main thread. For some reason, if the TCP connection happens to abruptly close while waitForBytesWritten or waitForReadyRead are in progress, I get a SIGSEGV. In debug mode it's preceded by what looks like a bunch of qt_asserts (but I'm not seeing the actual Qt code, just qt_assert in the stack), and in release mode it just crashes. I made sure that I'm not accessing the QTcpSocket object from anywhere other than the transfer thread, but the crash happens without fail, every time the TCP connection is closed by the remote side. What could be causing this?

    Here is a bit of code where the crash always seems to occur:

    @
    while (true)
    {
    l_bytesWritten = mp_socket->write(l_sendBuffer+l_bufferBytesWritten, l_bytesToSend-l_bufferBytesWritten);

            if (l_bytesWritten == -1)
            {
                emit UploadError();
                return;
            }
    
            l_bufferBytesWritten += l_bytesWritten;
    
            while (mp_socket->bytesToWrite() > 0)
            {
                bool l_result = mp_socket->waitForBytesWritten(); // <-- crash happens right here
                if (! l_result) { emit UploadError(); return; }
            }
    
            if (l_bufferBytesWritten == l_bytesToSend) break;
        }
    

    @

    Thanks, Nir

    edit: This is on Windows by the way.

    1 Reply Last reply
    0
    • N Offline
      N Offline
      nirslsk
      wrote on last edited by
      #2

      I figured it out! I had a moveToThread call to move the QTcpSocketObject to the transfer thread, which apparently is necessary to make the waiting functions work properly, but was failing with this application output (which I didn't notice):

      "QObject::moveToThread: Cannot move objects with a parent"

      All it took was unsetting the parent as suggested "here":http://www.bitchx.com/log/qt-f/qt-f-04-Mar-2010/qt-f-04-Mar-2010-00.php , and the crashes went away altogether:

      @
      UploadThread * lp_thread = new UploadThread (ip_socket, lp_file, l_transferReq);

      ip_socket->setParent(0);
      ip_socket->moveToThread(lp_thread);
      
      lp_thread->start();
      

      @

      Thanks, Nir

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DenisKormalev
        wrote on last edited by
        #3

        Good to hear that you solved your problem. Please do not forget to mark thread as [solved] :)

        1 Reply Last reply
        0
        • N Offline
          N Offline
          nirslsk
          wrote on last edited by
          #4

          Oh sorry, I was looking for some sort of 'answered' option, didn't realize there was a tag for that :) I'll be sure to do that next time.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DenisKormalev
            wrote on last edited by
            #5

            not only a tag. Please update title with [solved]

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved