释放一个继承于QTcpSocket的类内存失败
client_socket类声明
class client_socket:public QTcpSocket
{
Q_OBJECT
public:
explicit client_socket();
virtual ~client_socket(){}
signals:
void socket_disconnect(int socket_id);
private :
QString socket_ip;
int blocksize;
int socket_id;
};
//QList<client_socket*> socket_list;
服务器里面将发起连接的客户端socket加入socket_list
void server::incomingConnection(int handle)
{
qDebug()<<"client_menu"<<handle;
client_socket *socket = new client_socket();
socket_list.append(socket);
socket->init_socket(handle);
socket->setObjectName(QString::number(handle,10));
connect(socket,SIGNAL(socket_disconnect(int)),this,SLOT(del_connect(int)));
}
//在执行删除连接时释放类存总是失败???
void server::del_connect(int socket_id)
{
qDebug()<<"dellete client connect!"<<socket_id<<socket_list.count();
foreach(client_socket *m_socket,socket_list)
{
if(m_socket->objectName() == QString::number(socket_id,10))
{
socket_list.removeOne(m_socket);
m_socket->close();
qDebug()<<"address1"<<m_socket;
delete m_socket;//释放内存失败?????
}
}
}
报错:
ASSERT: "d" in file ..\..\include/QtCore/../../src/corelib/tools/qscopedpointer.h, line 112
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.
有人能解释下吗??? socket 内存
[解决办法]
给你分析下m_socket->close()都要做些啥事情:
首先会发射一个信号:aboutToClose()
然后断开与主机连接的套接字,关闭套接字,并重置名称、地址、端口号和潜在的套接字描述符。
再看下你的代码,当m_socket还在做这些事情的时候,你就delete m_socket了,所以会出问题。
而使用QOBJECT的deleteLater()函数,它会让所有事件都发送完一切处理好后再清除这片内存。