QTcpSocket -退出QRunnable时指定的句柄无效
QTcpSocket - An invalid handle was specified when exting QRunnable
这个错误发生在代码段:
void TCPConnectThread::run()
{
m_socket = new QTcpSocket();
m_socket->setSocketDescriptor(m_fd);
m_socket->waitForReadyRead(10000);
QString data = m_socket->readAll();
m_socket->waitForDisconnected();
}
if (::WSAIoctl(socketDescriptor, FIONREAD, &dummy, sizeof(dummy), &nbytes,
sizeof(nbytes), &sizeWritten, 0,0) == SOCKET_ERROR) <-Exception here
{
WS_ERROR_DEBUG(WSAGetLastError());
return -1;
}
更深层次:
if (::getsockopt(d->socketDescriptor, SOL_SOCKET,
SO_ERROR, (char *) &value, &valueSize) == 0) <-Here
退出run
方法时发生invalid handle
异常。
m_socket = new QTcpSocket();
m_socket->setSocketDescriptor(m_fd);//m_fd is the socket descriptor of another socket
//from another thread
下面是收集m_fd
的线程:
void TCPListenerThread::onNewConnection()
{
QTcpSocket *clientSocket = m_tcpServer->nextPendingConnection();
int sockfd = clientSocket->socketDescriptor();
m_connectThread = new TCPConnectThread(sockfd);
m_threadPool->start(m_connectThread);
}
异常:Most possible exception at 0x76edf9ea in manager_host.exe:
0xC0000008: An invalid handle was specified
如何以及在哪里可以找到这个无效句柄
不能使用QTcpSocket
对象的套接字描述符,如果它已经被另一个QTcpSocket
对象使用。一旦分配给QTcpSocket
即使你没有显式地使用初始的QTcpSocket
,如果在它被创建的线程中有一个事件循环(这可能是这里的情况),Qt将在该线程中监视它。
作为另一种选择,您可以:
- 派生
QTcpServer
类来重新定义它的incomingConnection(int socketDescriptor)
方法,以便在它被分配给QTcpSocket
之前获得描述符,而不是使用nextPendingConnection
或 直接将从
nextPendingConnection
接收到的QTcpSocket
而不是套接字描述符作为参数传递给线程构造器,并将其移动到另一个线程(参见注释):TCPConnectThread(QTcpSocket *socket) : m_socket(socket) { m_socket−>setParent(0); // necessary to move the object to another thread m_socket->moveToThread(this); ... }
由于移动必须从初始线程完成,第一个备选方案可能更容易使用
QRunnable
,因为您可能无法轻松访问可运行程序将使用的未来QThread
。
相关文章:
- 枚举进程模块在有效句柄上返回无效句柄
- 未知C++错误:致命错误:glibc检测到无效的stdio句柄
- 获取进程内存信息失败,错误 6 句柄无效
- 发布信号量返回错误 6(无效句柄)
- 如何测试 USB HID 句柄由于设备被重新插入而无效
- 为什么错误1400无效的窗口句柄
- 打电话给谁是无效的关闭手(句柄)是安全的吗?
- 在列表视图中启用分组 - 无效句柄
- DirectShow BindToObject返回无效句柄
- DoModal 返回 -1 Getlasterror 说句柄无效
- C++RegOpenKeyEx-错误6:句柄无效
- 错误 6 句柄在 Windows 上写入文件无效
- 错误 1400 无效的窗口句柄
- 关闭句柄调用上的句柄异常无效
- 调用IOCTLSTORAGE_GET_MEDIA_SERIAL_NUMBER时句柄无效
- 无效句柄(使用应用程序验证程序)
- AssignProcessToJobObject失败句柄无效
- QTcpSocket -退出QRunnable时指定的句柄无效
- 为什么未初始化的返回值会导致createwindowwex的无效窗口句柄错误
- Qt 5.8 windows打印机api无效句柄错误