本文介绍了QWebSocket底层的QTcpSocket(或QSslSocket)是否在发生套接字错误时被自身破坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

QTcpSocket(或QSslSocket)升级为QWebSocket时,前者必须存储以备将来使用,因为它需要...

When a QTcpSocket (or QSslSocket) is upgraded to a QWebSocket, the former has to be stored for the future purpose, as it requires to be ...

  1. 移动到moveToThread()所在位置的同一线程
  2. deleteLater()每当QWebSocket被破坏时
  1. moved to the same thread wherever QWebSocket is being moveToThread()
  2. deleteLater() whenever QWebSocket is being destroyed

不执行 1.会导致不确定的行为,并且很可能导致崩溃.未能执行 2.会导致内存泄漏,如果您使用的是QWebSocketServer().
我学习了这些困难的方法,因为它没有很好的记录. :-)

Failing to do 1. results in undefined behaviour and most likely a crash. While failing to do 2. results in a memory leak, which is more prominent if you are having a QWebSocketServer (QWebSocketServer - not releasing memory).
I learned these hard way, as it's not well documented. :-)

最近为我们的QWebSocketServer体系结构进行了代码重构.因此,我看到了一个奇怪的行为. ->每当来自远程客户端的错误(即QWebSocket::error()信号)出现时,基础QTcpSocket就会通过destroy()信号由外部发出,并且可能会被删除.
可能存在某些编码不匹配的可能性,这会导致这种情况,但IMO的可能性较小.因此,在不讨论代码细节的情况下,我想问一下...

Recently did code refactoring for our QWebSocketServer architecture. Due to that, I am seeing a strange behavior. --> Whenever there is an error from the remote client, i.e. QWebSocket::error() signal, the underlying QTcpSocket is emitted with destroy() signal by something external and is possibly deleted.
There might be some coding mismatch possibility, which causes this scenario, but IMO it's less likely. So without going into the code details, I wanted to ask...

问题:在QWebSocket的情况下,Qt框架在某些情况下是否破坏了基础QTcpSocket?

Question: In the context of QWebSocket, is the underlying QTcpSocket destroyed by the Qt framework in certain scenario?

推荐答案

出现Web套接字错误或断开连接时,Qt框架破坏了基础套接字.
由于Qt管理基础QTcpSocket的内存,因此 2.中的假设是错误的.

Upon web socket error or disconnection, the underlying socket is destroyed by Qt framework.
Since Qt manages the memory of underlying QTcpSocket, the assumption in 2. is wrong.

我发现有一个编码错误,我在较早的代码中没有有效地执行deleteLater().这给我的印象是,我们必须管理基础QTcpSocket.

I found out there to be a coding error, where I was not effectively performing deleteLater() in earlier code. Which gave me an impression that we have to manage the underlying QTcpSocket.

这篇关于QWebSocket底层的QTcpSocket(或QSslSocket)是否在发生套接字错误时被自身破坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-18 19:58