F libc : Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x8 in tid 18493 (QThread), pid 18454 (project.example)
或者是
ASSERT: “m_buf” in file
这两个错误 是因为socket 在write的时候, 调用线程和socket创建的线程不一致产生的崩溃 有两种情况 在其他线程调用了write函数,需要通过信号跳转到socket创建的线程调用write(connect信号时添加Qt::QueuedConnection参数),例如:
QObject::connect(this, static_cast<void (Socket::*)(const QByteArray &)>(&Socket::write),
this, [=](const QByteArray &byte){
if( socket->state() == QTcpSocket::ConnectedState ) {
socket->write(byte.data(), byte.size());
}
}, Qt::QueuedConnection);
处理socket的disconnected信号重连时,QObject::connect需要添加Qt::QueuedConnection 参数
QObject::connect(socket, &QTcpSocket::disconnected, this, [=](){
socket->disconnectFromHost();
qDebug() << "socket disconnect";
int time = 0;
socket->connectToHost(deviceIP, cfg.port);
while (!socket->waitForConnected(50) && !exit && (time < 20)) {
socket->connectToHost(cfg.ip, cfg.port);
time ++;
}
if( socket->state() != QAbstractSocket::ConnectedState ) {
qDebug() << "socket reconnect fail";
}
else {
qDebug() << "socket reconnect success";
}
}, Qt::QueuedConnection);
|