QNetworkAccessManger is not ok for mem
-
Que: my Code is this,QT Ver is QT5.9.4
ohh,My program encountered a problem。
in slots function replyFinished . reply-deleter() is called before return. but the mem continues to increase。
I expect for your reply 。thanks.QNetworkReply *replay=http->post(req, d); connect(replay,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinished(QNetworkReply*))); replyTimeout *reply_timer=new replyTimeout(replay,5000);
void httpRequest::replyFinished(QNetworkReply *reply) { QString r_url; r_url=reply->url().toString(); QByteArray r_byte; r_byte.clear(); if(reply->error() != QNetworkReply::NoError) { r_byte=reply->errorString().toUtf8(); send_notify_err(r_url,1); qDebug("QNetworkReply::err %d",reply->error()); reply->deleteLater(); return ; } r_byte= reply->readAll(); if (r_byte.isEmpty()) { r_byte=QString("isEmpty").toUtf8(); send_notify_err(r_url,2); reply->deleteLater(); qDebug()<<"r_content isEmpty()"; return ; } // m_callbackFunc(&r_url,&r_byte); reply->deleteLater(); send_notify_msg(r_url,r_byte); }
-
@test_song said in QNetworkAccessManger is not ok for mem:
replyTimeout *reply_timer=new replyTimeout(replay,5000);
What do you use this for? In the code you posted you do not delete it.
-
@test_song said in QNetworkAccessManger is not ok for mem:
reply_timer
this reply_timer is a class to monitor ,reply is it's father, it deleted with reply erased
class replyTimeout : public QObject {Q_OBJECT
public:
replyTimeout(QNetworkReply *reply, const int timeout) : QObject(reply) {
Q_ASSERT(reply);
if (reply && reply->isRunning()) { // 启动单次定时器
QTimer::singleShot(timeout, this, SLOT(onTimeout()));
}
}
~replyTimeout()
{
qDebug()<<"~replyTimeout";
}
private slots:
void onTimeout() { // 处理超时
QNetworkReply reply = static_cast<QNetworkReply>(parent());
if (reply->isRunning()) {
qDebug() << "abort reply "<< reply->url().toString();
reply->abort();
}
}
};