QWebSocketServer-不释放内存 [英] QWebSocketServer - not releasing memory

查看:705
本文介绍了QWebSocketServer-不释放内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我在安全的Websocket服务器应用程序上运行valgrind并发现了一个问题(在Qt Memcheck中,我必须检查外部错误" 才能看到它)-一些字节明确丢失了 -指向我的main呼叫所在的位置.在调用之后的堆栈跟踪中,只有一些Qt库,以libQt5Core开始,以libQt5WebSockets结尾.丢失的字节数随建立的连接数而增加.我再次检查是否删除了所有由QWebSocketServer::nextPendingConnection()返回的QWebSocket实例.我也删除了QWebSocketServer实例.有趣的是,当运行 sslechoserver 演示程序或在不安全模式(无SSL)下运行服务器时,valgrind不会报告任何错误.我不知道这是否是实际问题.

First, I ran valgrind on my secure websocket server application and found an issue (in Qt Memcheck I had to check "External Errors" to see it) - some bytes are definitely lost - pointing to my main just where my QCoreApplication::exec() call is. In the stack trace, after the call, there are just some Qt libraries, starting with libQt5Core and ending with libQt5WebSockets. The number of lost bytes increases with the number of connections made. I double-checked that I delete all QWebSocket instances returned by QWebSocketServer::nextPendingConnection(). I do also delete the QWebSocketServer instance. Interesting thing is, that valgrind does not report any error when running sslechoserver demo nor when running my server in unsecure mode (no SSL). I don't know if this is an actual issue.

第二,更重要的是,我观察到消耗的内存随着每个新连接的增加而不断增加(即使旧连接已关闭并且其QWebSocket实例也已删除) ,实际上大多数情况下,我的应用程序和 sslechoserver 演示中都存在0或1个活动连接).我在Ubuntu上使用 systemmonitor ,在Windows上使用 Windows Task Manager 进行观察.建立数万个连接后,报告的内存使用量为数十兆字节,而在应用程序生命周期开始时约为1.5 MB.

Secondly, more importantly, I am observing that consumed memory keeps increasing with every new connection (even though the old connections are closed and their QWebSocket instances are deleted, actually most of the time there is 0 or 1 active connection) in both my application and the sslechoserver demo. I used systemmonitor on Ubuntu and Windows Task Manager on Windows for the observation. The reported memory usage are dozens of megabytes after tens of thousands connections were made, while at the beginning of the application's lifecycle it was ~1.5 MB.

我用Qt 5.5.1和5.6.0(Ubuntu上的GCC,Windows上的MSVC)对其进行了测试.

I tested it with Qt 5.5.1 and 5.6.0 (GCC on Ubuntu, MSVC on Windows).

我弄错了吗? Qt的websockets中有错误吗? sslechoserver 演示不正确吗?

Am I getting something wrong? Is there a bug in Qt's websockets? Is the sslechoserver demo incorrect?

更新:我也尝试了 echoserver 演示(没有SSL),并且消耗的内存也在不断增加.

UPDATE: I did also try echoserver demo (without SSL) and the consumed memory keeps increasing there too.

更新2:我们得出的结论是Qt中肯定有一个错误.错误报告在这里: https://bugreports.qt.io/browse/QTBUG-53503

UPDATE 2: We concluded there must be a bug in Qt. The bug report is here: https://bugreports.qt.io/browse/QTBUG-53503

推荐答案

更新:我怀疑基础QTcpSocket是否必须由用户管理.但是,从Qt 5+开始,这似乎并不正确.请参阅:是是QWebSocket底层的QTcpSocket(或QSslSocket),当发生套接字错误时会被自身破坏吗?.
答案的不正确部分在下面删除.

Update: I had a doubt that the underlying QTcpSocket had to be managed by the user. However, it seems to be not true as of Qt 5+. Please refer: Is the QTcpSocket (or QSslSocket) which underlies a QWebSocket -- destroyed by itself when a socket error happens?.
The incorrect part of the answer is striked-through below.

当我们使用QWebSocketServer::handleConnection()方法将QTcpSocket升级到QWebSocket时,两个套接字都在不同的内存地址上保持活动状态!

When we upgrade a QTcpSocket to a QWebSocket using QWebSocketServer::handleConnection() method, both the sockets remain active on different memory addresses!

换句话说,QWebSocket充当包装器,并且所有套接字通信都通过基础的QTcpSocket进行. 因此,我们管理的每个1个Web套接字中,还有1个隐藏的TCP套接字.

In other words, the QWebSocket acts as a wrapper and all the socket communication happens via the underlying QTcpSocket. So for every 1 Web socket we manage, there is 1 more hidden TCP socket.

当要销毁两个套接字时,都应使用deleteLater()机制对其进行处理.
这种情况至少对于从TCP到Web的升级都是正确的.

Both the sockets should be handled using deleteLater() mechanism when they are meant to be destroyed.
This scenario is true at least for the TCP to Web upgradation.

这篇关于QWebSocketServer-不释放内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆