Solved Memory Leak on QNetworkAccessManager
-
Hi @aha_1980 .
I did it this way because the QNetworkAccessManager is asynchronous. I need to get it to wait for finalization or receive a timeout after a certain time, in the example case, 10 seconds.
Could guide me if there's a better way to do this?
-
Really the Windows task manager is not the best tool to check this problem. But I did not need another tool to identify that there is a leak, if there is one. It is very strange to see the program consuming RAM this way, this in the long run can be disastrous.
-
I'd use a QTimer with 10 seconds timeout. When timer slot is fired, check if the request is finished, otherwise abort it.
Am I missing something?
-
True, I had forgotten that part. I'll do this and see if the problem continues.
-
@Maickonn-Richard said in Memory Leak on QNetworkAccessManager:
Really the Windows task manager is not the best tool
It's not even a tool to catch such kind of errors in any way. It may be a hint but nothing more... and since valgrind (which is a tool to catch exactly such kind of problems) does not show anything... but do whatever you like.
-
I thank you very much for your help with Valgrind! unfortunately currently I can not access this tool.
-
If I may,
this is one way, how you can make use of the assynchon function:
QString getHTML(QString url) { QNetworkRequest request; request.setUrl(QUrl(url)); request.setRawHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"); request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true); QNetworkAccessManager *manager = new QNetworkAccessManager(this); connect(manager, &QNetworkAccessManager::finished, this, [=](QNetworkReply *reply){ if(reply.error() == QNetworkReply::NoError) emit htmlRead(QString(reply->readAll()); else emit htmlError(reply->error()); manager->deleteLater(); reply->deleteLater(); }); manager->get(request); QTimer *timeout = new QTimer(this); timeout->start(10000); connect(timeout &QTimer::timeout, manager, [=]{ emit htmlTimeout(); manager->deleteLater(); timeout->deleteLater(); }); connect(manager, &QNetworkAccessManager::finished, timeout, QTimer::deleteLater); }
You'll have to create slots that connect to the signals
void htmlTimeout()
,void htmlError(QNetworkReply::NetworkError)
andvoid htmlRead(QString)
and proceed from there.This should work, but it's untested.
-
@J.Hilk very nice example!
And it perfectly shows how well signals works together with lambdas...
-
@aha_1980
thanks, sadly I leaked theQNetworkReply *reply
in the example x).
Fixed it. -
Well guys, after leaving the software running for a long period with the code I posted, I noticed that the consumption of RAM stabilizes. That is, leaks are not happening.
I thank all who cooperated, the help of you all were immense, you gave me a number of alternative ways in which I had not considered before.
A big hug to everyone!
-
@aha_1980 Can you explane it. I have same problem with method PUT. Thank you so much!
-
@May-May Hi!
Explain what?