July 13, 2011

nirslsk nirslsk
Lab Rat
11 posts

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

 

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:

  1.         while (true)
  2.         {
  3.             l_bytesWritten = mp_socket->write(l_sendBuffer+l_bufferBytesWritten, l_bytesToSend-l_bufferBytesWritten);
  4.  
  5.             if (l_bytesWritten == -1)
  6.             {
  7.                 emit UploadError();
  8.                 return;
  9.             }
  10.  
  11.             l_bufferBytesWritten += l_bytesWritten;
  12.  
  13.             while (mp_socket->bytesToWrite() > 0)
  14.             {
  15.                 bool l_result = mp_socket->waitForBytesWritten(); // <-- crash happens right here
  16.                 if (! l_result) { emit UploadError(); return; }
  17.             }
  18.  
  19.             if (l_bufferBytesWritten == l_bytesToSend) break;
  20.         }

Thanks, Nir

edit: This is on Windows by the way.

4 replies

July 14, 2011

nirslsk nirslsk
Lab Rat
11 posts

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 [bitchx.com] , and the crashes went away altogether:

  1.     UploadThread * lp_thread = new UploadThread (ip_socket, lp_file, l_transferReq);
  2.  
  3.     ip_socket->setParent(0);
  4.     ip_socket->moveToThread(lp_thread);
  5.  
  6.     lp_thread->start();

Thanks, Nir

July 14, 2011

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

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

July 14, 2011

nirslsk nirslsk
Lab Rat
11 posts

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.

July 14, 2011

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

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

 
  ‹‹ When to delete QThread      QGridLayout: widgets don’t align properly ››

You must log in to post a reply. Not a member yet? Register here!