[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:
@
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.
-
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
-
Good to hear that you solved your problem. Please do not forget to mark thread as [solved] :)
-
not only a tag. Please update title with [solved]