如何安全删除一个QT::QTcpSocket?
How to safely delete a QT::QTcpSocket?
QTcpSocket * QTcpServer::nextPendingConnection () [virtual]
The socket is created as a child of the server, which means that it is
automatically deleted when the QTcpServer object is destroyed. It is
still a good idea to delete the object explicitly when you are done
with it, to avoid wasting memory.
在我的应用程序中,QTcpServer
存在很长时间(它在连接关闭时与主机断开连接,但除非程序退出,否则永远不会被销毁),并且它接受很多 QTcpServer::nextPendingConnection
并占用大量内存。
如何在切换到下一个挂起之前删除旧的QTcpSocket对象以节省内存,同时避免重复删除?
在这种情况下 delete
可以吗?
Is delete ok in this case?
是的,感谢 Qt 的对象设计的巧妙。
特别是QTcpSocket(最终)派生自Object,QObject的析构方法最后包含这段代码:
if (d->parent) // remove it from parent object
d->setParent_helper(0);
因此删除 QTcpSocket 对象会自动从其父对象(在本例中为 QTcpServer)的子列表中删除 QTcpSocket,因此当 QTcpServer 对象被销毁时不会出现双重删除。
QTcpSocket * QTcpServer::nextPendingConnection () [virtual]
The socket is created as a child of the server, which means that it is automatically deleted when the QTcpServer object is destroyed. It is still a good idea to delete the object explicitly when you are done with it, to avoid wasting memory.
在我的应用程序中,QTcpServer
存在很长时间(它在连接关闭时与主机断开连接,但除非程序退出,否则永远不会被销毁),并且它接受很多 QTcpServer::nextPendingConnection
并占用大量内存。
如何在切换到下一个挂起之前删除旧的QTcpSocket对象以节省内存,同时避免重复删除?
在这种情况下 delete
可以吗?
Is delete ok in this case?
是的,感谢 Qt 的对象设计的巧妙。
特别是QTcpSocket(最终)派生自Object,QObject的析构方法最后包含这段代码:
if (d->parent) // remove it from parent object
d->setParent_helper(0);
因此删除 QTcpSocket 对象会自动从其父对象(在本例中为 QTcpServer)的子列表中删除 QTcpSocket,因此当 QTcpServer 对象被销毁时不会出现双重删除。