我正在做一个链接检查应用程序,它在while循环中执行了很多http请求,我在 Windows任务管理器中注意到我的应用程序随着时间的推移使用了超过1600个线程并且数量永远不会下降,直到它崩溃为止该应用程序. (我猜这是原因.)
我的问题是,QNetworkAccessManager是否有使用线程池的选项?
或者它有完成其http请求后清除其线程的选项?
这是主循环:
while(!rpm_urlStack->isEmpty()){ QString url = rpm_urlStack->top(); //define the reply QNetworkReply *reply; rpm_urlStack->pop(); QString urlForReq(url); bool returnVal = true; qnetworkrequest request; request.setUrl(QUrl(urlForReq)); request.setRawheader("User-Agent",USER_AGENT.toUtf8()); request.setRawheader("Accept-Charset","ISO-8859-1,utf-8;q=0.7,*;q=0.7"); request.setRawheader("Accept","text/HTML,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); request.setRawheader("Accept-Language","en-us,en;q=0.5"); request.setRawheader("Connection","Keep-Alive"); QEventLoop loop; reply = m_networkManager->get(request); connect(reply,SIGNAL(finished()),&loop,SLOT(quit())); loop.exit(); if(!loop.isRunning()) { loop.exec(); } RequestFinishedHandler(reply); // this is how I delete the reply object delete reply;}RequestFinishedHandler(QNetworkReply *reply){ if (reply->error() > 0) { QNetworkReply::NetworkError networkError = reply->error(); QString err = reply->errorString(); } else { QVariant vstatusCodeV = reply->attribute(qnetworkrequest::httpStatusCodeAttribute); qmutexLocker lock(_pMutex); // _pMutex defined as class member char *buffer; buffer = getCurrentDateTime(); QTextStream out(m_file); out << buffer << " " << _sCurrentUrl << "\n"; lock.unlock(); if(vstatusCodeV.toInt() == 200) { QString APIResponse; QByteArray data; data=reply->readAll(); APIResponse.append(QString::fromUtf8(data)); } }}解决方法 要使有效的 appears,必须从事件循环中调用deleteLater方法,该循环必须重新获得对执行的控制以处理垃圾收集.
也许你应该重构你的代码,让事件循环取代你的while循环.或者,由于您没有使用完成的槽来处理回复,因此您可以直接在RequestFinishedHandler函数的末尾删除回复.
总结以上是内存溢出为你收集整理的c – QNetworkAccessManager线程永远不会完成全部内容,希望文章能够帮你解决c – QNetworkAccessManager线程永远不会完成所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)