Qt QNetworkAccessManager请求返回reply内存泄漏

做项目http请求时发现内存泄漏,原来QNetworkAccessManager请求返回的reply需要删除,有两种方法删除:

1.在finish槽函数删除reply,需要使用deleteLater而不是delete

QNetworkReply *reply = m_pManager->get(request);
connect(reply,SIGNAL(finished()),this,SLOT(finished()));

 

void finished()

{

if (reply)
    {
        reply->deleteLater();
    }

}

2.在事件循环后删除

QNetworkReply *reply = m_pManager->get(request);
connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
eventLoop.exec();    

if (reply)

{

reply->deleteLater();

}

 

其实官方文档也有说明,如下:

你可能感兴趣的:(Qt)