PDA

View Full Version : Do QTcpSocket or QSslSocket automatically create thread for reading/writing?



iammilind
13th July 2017, 11:41
Despite not using `std::thread` or `QThread` anywhere, still getting following problems:
1. Always a runtime debug error log from Qt:

QObject::connect: Cannot queue arguments of type 'QAbstractSocket::SocketError'
(Make sure 'QAbstractSocket::SocketError' is registered using qRegisterMetaType().)


2. Intermittent crash on `TcpSocket::flush()` method; I use this method to make sure that the TCP is written immediately; Now sometimes the app crashes exactly at this method with `SIGPIPE`

Upon searching internet, found that people suggest that to fix 1st problem (i.e. the meta error), I need to register using qRegisterMetaType(), when we have multiple threads.
Same multithreading is referred as a cause for the 2nd problem as well; see this (https://forum.qt.io/topic/32756/cannot-write-with-qtcpsocket) and this (http://www.qtcentre.org/threads/59138-QTcpSocket-crash-on-write).

But I don't have more than 1 thread!
My socket code looks like below:


struct Socket : public QSslSocket
{
Q_OBJECT public:

void ConnectSlots ()
{
const auto connectionType = Qt::QueuedConnection;
connect(this, SIGNAL(readyRead()), this, SLOT(ReceiveData()), connectionType);
connect(this, SIGNAL(disconnected()), this, SLOT(Disconnected()), connectionType);
connect(this, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(Error(QAbstractSocket::SocketError)), connectionType);
// ^^^^^^^ error comes whether I comment this or not
}

public slots:
void ReceiveData () { ... }
void Disconnected () { ... }
void Error () { ... }
}

Question: Is Qt creating any internal thread by itself for read/write purpose? (I hope not). How to fix above 2 issues?

high_flyer
13th July 2017, 14:30
Registering the enums is a must if you want them to be used with signal/slots.
It has nothing to do with threads.

Is Qt creating any internal thread by itself for read/write purpose? (I hope not)
No, they don't.